2

I am new in scala and reading Programming in scala. I find the terms function type, function value and function objects in various pages in the book. For instance:

For instance, where other languages might have objects and functions as two different concepts, in Scala a function value is an object. Function types are classes that can be inherited by subclasses.

Can you please explain function type, function value and function objects and the differences between them? Also what does the above quote mean?

Neha Sharma
  • 295
  • 1
  • 2
  • 12

4 Answers4

3

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.

Ra Ka
  • 2,995
  • 3
  • 23
  • 31
2

In Scala functions are no different from "ordinary" values.

val inc: Int => Int = x => x + 1

is the same concept as

val s: String = "hello world"

For any val x: T = e, x is the name of the value whose type is T, and x is the result of evaluating e.

inc is a value/object just like s is a value/object. I think value and object are essentially the same thing here.

Ziyang Liu
  • 810
  • 4
  • 10
0
val f: Int => String = i.toString
  • f is a value that holds a reference to a function, I guess it could be called a function value.
  • Int => String is a function type, a type that corresponds to a function from integer to string.
  • f is also an object (I've never read function object), you can for instance call f.toString and f.hashCode, like you would on any other object.
OlivierBlanvillain
  • 7,701
  • 4
  • 32
  • 51
0

Practically, a Scala programmer only needs to know the following three rules to use functions and function values properly:

  • Methods defined by def and function literals defined by => are functions. It is defined in page 143, Chapter 8 in the book of Programming in Scala, 4th edition.
  • Function values are objects that can be passed around as any values. Function literals and partially applied functions are function values.
  • You can leave off the underscore of a partially applied function if a function value is required at a point in the code. For example: someNumber.foreach(println)

In the Programming in Scala book, object and value are used interchangeably. Therefore, function value and function object are the same thing.

Ying
  • 2,660
  • 24
  • 23