Saturday, 7 September 2013

Scala anonymous function genric variance issues

Scala anonymous function genric variance issues

I'm on the road to learn Scala and I'm having a hard time understanding
contravariants, covariants, invariance, etc. From Scala covariance /
contravariance question I have learned how functions can be considered
subtypes of another function. (Really useful to know!)
The code below is what I believe are the important pieces to solving my
puzzle. I have extracted parts that I think would add unneeded complexity
to the problem. According to the example I have a Student object that will
act as a factory to generate functions.
Functions will take in types or subtyes of AnyVal (Int, Double, Long,
etc.), and the return output will be of the same input type. To achieve
this the student class takes in a generic (A) that is a subtype of AnyVal.
The abstract class is there so that I can refer a list of these students
by doing something like List[Master[AnyVal]]( Student.func1).
The problem is that I cannot have the line "val function: List[A] => A" as
I get error "covariant type A occurs in contravariant position in type =>
List[A] => A of value function". I don't know why the return type must be
the contravariant of A. I can somewhat accept this fact for name based on
the Function1 trait.
So how would I define my function in the abstract Master class so that the
return type is a contravariant of type A? I found an example of how to
define this with function definitions (Ex. def function[B >: A](v: B):
List[B]), but how would I implement this with an anonymous function?
Remember "A" in the master abstract class must be covariant since there
will be a list of function that take in all AnyVal types (Int, Double,
etc.)
Really appreciate the help! Let me know if any of my terminology is off. -
Scala Learner
abstract class Master[+A] {
val function: List[A] => A
}
class Student[A <: AnyVal](val function: List[A] => A) extends Tfunc[A]
object Student {
def func1 =
new Function((params: List[Int])=>params(0) + params(1))
}
val myFunc = Student.func1
val someLisst = List[Master[AnyVal]](myFunc)

No comments:

Post a Comment