Scala: problems using functions as first class objects

I need to have a set of common functions, but I can't do it the way I like it. I created

List[(Any)=>Unit]

but as soon as I try to insert a function, for example,

String=>Unit

I get an error message. How can I declare a common set of functions that does not take into account parameter types and return values?

+5
source share
2 answers

To complete the answer @Moritz, you need to select a type argument for T1, which is a subtype of the input type of each function in the list. Nothingsuitable for counting - this is a subtype of each type.

scala> val l: List[Nothing => Any] = List((b: String) => b, (a: Int) => a)
l: List[(Nothing) => Any] = List(<function1>, <function1>)

There is also an existential type:

scala> val l: List[_ => _] = List((b: String) => b, (a: Int) => a)        
l: List[Function1[_, _]] = List(<function1>, <function1>)
+9
source

, . Function1[-T1,+R]. , Any => Unit List[String => Unit], . , , , , String Any.

+11

All Articles