Scala: issues using functions as first class objects
I need to have a collection of generic functions, but I can't get it done in the way I like. I created a
List[(Any)=>Unit]
but as soon as I try to insert a function, for example a
String=>Unit
I get an er开发者_开发知识库ror. How could I declare a generic function collection that does not consider parameter and return values types?
Functions are contravariant in the intput type parameters, e.g. in your case Function1[-T1,+R]
. This means you can add an instance of Any => Unit
to a List[String => Unit]
but not the other way round. This of course makes sense as you cannot call a function expecting an argument of type String
with an argument of type Any
.
Just to finish of @Moritz
's answer, you need to choose type argument for T1
that is a subtype of the input type of every function in the list. Nothing
fits the bill -- it is a subtype of every type.
scala> val l: List[Nothing => Any] = List((b: String) => b, (a: Int) => a)
l: List[(Nothing) => Any] = List(<function1>, <function1>)
An existential type also works:
scala> val l: List[_ => _] = List((b: String) => b, (a: Int) => a)
l: List[Function1[_, _]] = List(<function1>, <function1>)
精彩评论