1

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?

gobernador
  • 5,659
  • 3
  • 32
  • 51
  • 5
    It isn't accurate to say that "everything is an object"; methods aren't objects. * [Methods and functions are not the same thing.](https://stackoverflow.com/questions/2529184/difference-between-method-and-function-in-scala/2530007) * [Anonymous functions can't be generic.](https://stackoverflow.com/questions/2554531/how-can-i-define-an-anonymous-generic-scala-function) – Chris Martin Jun 01 '14 at 22:00
  • 3
    To build on what Christ Martin said, `def` is used to declare methods, *not* functions. – wingedsubmariner Jun 01 '14 at 22:53

0 Answers0