Illiax
Illiax

Reputation: 992

Scala casting to function

For some reason i have an abstract class with the method def apply(some : Some) : Any. One of the subclasses in particular returns a partially applied function like

 return twoArgumentFunction(_ : Int, 1)

but the signature of apply is Any. The question is how to cast this to a function of one argument?

def usingApply(){ var f = A.apply() ; f(4)}

i was expecting some asInstanceOf() or something like that

Upvotes: 1

Views: 728

Answers (1)

DaoWen
DaoWen

Reputation: 33019

I think you're just confused about the syntax for asInstanceOf. You provide the target type in square brackets (not parentheses), like any other generic type argument:

def usingApply(){ var f = A.apply().asInstanceOf[Int=>Int] ; f(4)}

That would cast the result of apply to Int=>Int, which is the same as Function1[Int,Int].

Upvotes: 2

Related Questions