Mid-Term Exam WS 15/16 Task 1d)

Moderatoren: pmueller, SE - Design and Construction

Eternum
Neuling
Neuling
Beiträge: 6
Registriert: 27. Jun 2013 17:49

Mid-Term Exam WS 15/16 Task 1d)

Beitrag von Eternum » 13. Jul 2018 14:23

I would like some input on the following task:

Code: Alles auswählen

Take a look at the following Scala code example:

class MySuperclass
class MyClass extends MySuperclass
class MySubclass extends MyClass
//As a reminder:
//Boolean <: AnyVal <: Any
//everything <: Any
//Nothing <: everything
//Null <: every AnyRef
val f1: (Any) => Boolean = ???
val f2: (Seq[AnyRef]) => Boolean = ???
val f3: (Any) => Any = ???
val f4: (Seq[MySubclass]) => Boolean = ???
val f5: (Seq[MySuperclass]) => Null = ???
val f6: (List[_]) => AnyVal = ???
val f: (Seq[MyClass]) => Boolean = // f1 ?, f2 ?, ..., f6 ?

The last line is just a short hand for assigning each variable f1 ... f6 to f .

Now state for each fi if this assignment is valid. (3P)

Discuss for one case why it succeeds or why it is rejected by the compiler. Use the terminology covariance and contravariance. (2P)

My thoughts:

To my unterstanding any assignment that succeeds must:
- Have an input parameter that is either of the type Seq[MyClass] or a supertype of it
- Have an output that is either of the type Boolean or a subtype of it

f1: This one should succeed. Any is obviously a supertype of Seq[MyClass] and Boolean is a perfect match for the output.
f2: This should also succeed since Seq[AnyRef] is (due to covariance of Seq) a supertype of Seq[MyClass] and Boolean is again a perfect match.
f3: This assignment should fail, since Any is not a subtype of Boolean.
f4: This one should fail again, since Seq[MySubclass] is not a supertype of Seq[MyClass]
f5: This assignment should also fail. Seq[MySuperclass] is a supertype of Seq[MyClass] (covariance of Seq), but Null is not a subtype of Boolean
f6: This one should also fail. List is not a supertype of Seq - no matter the type of the Seq / List.

My questions:
Are the given answers correct / sufficient?
Is contravariance a factor in this task or is it just mentioned to confuse?

typ
Windoof-User
Windoof-User
Beiträge: 32
Registriert: 5. Nov 2016 19:06

Re: Mid-Term Exam WS 15/16 Task 1d)

Beitrag von typ » 13. Jul 2018 16:00

In this task I would start with a sentence, what must be met, to have a valid line. Just to have the vocabularies 'contravariant' and 'covariant' in there.
As described here:
Another example that can help one understand variance is trait Function1[-T, +R] from the Scala standard library. Function1 represents a function with one argument, where the first type parameter T represents the argument type, and the second type parameter R represents the return type. A Function1 is contravariant over its argument type, and covariant over its return type. For this example we’ll use the literal notation A => B to represent a Function1[A, B].
from https://docs.scala-lang.org/tour/variances.html

'The parameters of a function are contravariant and the return type is covariant.' Then I would use one of your "fx:..." sentences for one case
Zuletzt geändert von typ am 13. Jul 2018 18:10, insgesamt 1-mal geändert.

Eternum
Neuling
Neuling
Beiträge: 6
Registriert: 27. Jun 2013 17:49

Re: Mid-Term Exam WS 15/16 Task 1d)

Beitrag von Eternum » 13. Jul 2018 16:44

Ah, of course.. I used the implication of co- and contravariance on functions for my general thought process, but I didn't think of actually spelling them out - was way too focused on List, Seq and the given classes.

Thanks a lot!

Benutzeravatar
AizazZaidee
BASIC-Programmierer
BASIC-Programmierer
Beiträge: 106
Registriert: 20. Apr 2016 22:49

Re: Mid-Term Exam WS 15/16 Task 1d)

Beitrag von AizazZaidee » 14. Jul 2018 08:56

They all are working, I tried in side scala Worksheet. :? :shock: :shock:

full size image => https://ibb.co/dxG5p8
Dateianhänge
Capture.JPG
output
Capture.JPG (32.61 KiB) 726 mal betrachtet

VSchü
Windoof-User
Windoof-User
Beiträge: 26
Registriert: 5. Jul 2015 16:46

Re: Mid-Term Exam WS 15/16 Task 1d)

Beitrag von VSchü » 14. Jul 2018 12:09

No.

Code: Alles auswählen

val f: (Seq[MyClass]) => Boolean = f1;f2;f3;f4;f5;f6
actually means:

Code: Alles auswählen

val f: (Seq[MyClass]) => Boolean = f1
 f2
 f3
 f4
 f5
 f6
You only assign f1 to f (which works as expected) and just output the values of f2 to f6. When you actually assign each one to f, you get exactly the results predicted in the first post.

typ
Windoof-User
Windoof-User
Beiträge: 32
Registriert: 5. Nov 2016 19:06

Re: Mid-Term Exam WS 15/16 Task 1d)

Beitrag von typ » 14. Jul 2018 14:28

AizazZaidee hat geschrieben:
14. Jul 2018 08:56
They all are working, I tried in side scala Worksheet. :? :shock: :shock:

full size image => https://ibb.co/dxG5p8
try that:

Code: Alles auswählen

    class MySuperclass
    class MyClass extends MySuperclass
    class MySubclass extends MyClass

    val f1: Any => Boolean = null
    val f2: Seq[AnyRef] => Boolean = null
    val f3: Any => Any = null
    val f4: Seq[MySubclass] => Boolean = null
    val f5: Seq[MySuperclass] => Null = null
    val f6: List[_] => AnyVal = null

    val g1: Seq[MyClass] => Boolean = f1
    val g2: Seq[MyClass] => Boolean = f2
    val g3: Seq[MyClass] => Boolean = f3
    val g4: Seq[MyClass] => Boolean = f4
    val g5: Seq[MyClass] => Boolean = f5
    val g6: Seq[MyClass] => Boolean = f6

Antworten

Zurück zu „Software Engineering - Design and Construction“