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

Verfasst:

**13. Jul 2018 14:23**I would like some input on the following task:

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?

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)
```

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?