Let me start with non-function type, value and object.
scala> val foo:String = "bar"
foo: String = bar
Here, type
is String
, value
is bar
and object
instance is foo
. Note that, value
is essentially an object of type String. For example, (just to get the idea between value and object):
scala> val foo_copy = foo
foo_copy: String = bar
Here, object foo
is assigned to foo_copy
. Since foo
is object in above case, it is a value for object foo_copy
.
Now, let me come to function example:
In scala, function is a value and you can assign it to variable.
scala> val foo: (String, String) => String = (a:String, b:String) => a + b
foo: (String, String) => String = <function2>
Here, function type is (String, String) => String
(take 2 string parameter and return value of type string) and expression (a:String, b:String) => a + b
is a function literal which is compiled into a class and then it is instantiated at runtime as function value. In code above, <function2>
is a function value which is essentially an object. Note, don't confuse function value with value that you get from this function invocation.
Finally, foo
is a function object of type (String, String) => String
and here as well both function object and function value are same. Note that, function value is instance of some class that extends to FunctionN traits. That means:
type: (String, String) => String = Function2[String, String] //2 - number of parameter
Therefore, a class can extends to a function type. class A extends ((String, String) => String)
is actually translated to class A extends Function2[String, String]
by Scala. Furthermore, a function type can be used as parameter type in function and these functions are higher ordered functions.