In Scala, everything is an object. Even functions are subclasses of the FunctionN
classes. We can declare functions with the following syntax:
def foo(a:A, b:B):C = ...
where A, B, and C are types. Given that the function foo
is a subclass of Function2[A,B,C]
, is it equivalent to say
val foo = new Function2[A,B,C] { def apply(a:A, b:B) = ... }
Furthermore, if so, how can generic functions such as
def bar[T](data:T)
be written using val
syntax?