Friday, 30 August 2013

What is the supertype of all functions in Scala?

What is the supertype of all functions in Scala?

I know I can do instanceOf checks against Function1 or Function2 etc but
is there a generic way to see if something is function or not (it can have
arbitray number of args). I tried defining something like this:
type FuncType = (Any*) -> Any
But that did not work either. Basically I have some code that looks like
this:
call = (name: Any, args: Any*) -> if name.isFunction then
name.castAs[Function].apply(args) else name
aFunction = (name: String) => "Hello " + name
notAFunction = "Hello rick"
call(aFunction, "rick")
call(notAFunction)

No comments:

Post a Comment