keed
keed

Reputation: 11

Can functions in Scala be defined with def?

I have found online resources saying functions can be defined with the term def, but would that not make the function a method then? Can functions be defined with val and def? Also can functions be defined inside a Singleton or Companion object since methods are inside classes to be called on the class instances, or where can the functions be defined?

Upvotes: 1

Views: 247

Answers (1)

Jörg W Mittag
Jörg W Mittag

Reputation: 369556

I have found online resources saying functions can be defined with the term def, but would that not make the function a method then?

Yes, def defines methods, not functions in Scala. Unless you are talking about local functions, which are nested in methods:

def thisIsAMethod = {
  val outerVal = 42

  def thisIsANestedLocalFunction = {
    println("Hello from local function")
    outerVal
  }

  thisIsANestedLocalFunction
  thisIsANestedLocalFunction
}

thisIsAMethod
// Hello from local function
// Hello from local function
//=> val res: Int = 42

Can functions be defined with val and def?

They can be defined neither with val nor def. val defines a variable, def defines a method. Neither defines a function. Functions aren't really defined at all, they are instantiated, since they are just objects like any other object.

You wouldn't say that "Hello" "defines a string", it simply "instantiates" (or "creates" or maybe "evaluates to") a string. The same with functions. (x: Int) => 2 * _ doesn't "define" a function, it instantiates it.

Also can functions be defined inside a Singleton or Companion object since methods are inside classes to be called on the class instances, or where can the functions be defined?

Again, functions aren't "defined". They are just objects like any other object. You can have a function everywhere you can have any a string, a list, an integer, whatever.

[…] methods are inside classes to be called on the class instances […]

This is wrong. Methods can be defined in:

  • classes
  • traits
  • objects
  • package objects
  • scripts
  • … and I might be forgetting something

Back to the original question:

I have found online resources saying functions can be defined with the term def

You shouldn't believe everything you read online ;-)

But, seriously, joking aside, I can imagine a couple of reasons why someone would claim something like that:

  • They don't understand the difference between methods and functions.

  • They do understand the difference between methods and functions, but call methods "functions" anyway.

  • They are referring to methods that return functions, e.g. something like:

    def thisIsAMethod(a: Int, b: Int) = (a, b) => _ + _
    

    This is a method that returns a function. This is not a function defined with def. The fact that methods can return functions is a trivial statement. Functions are objects, and of course, methods can return objects, otherwise they wold be useless.

  • Even the Scala Language Specification uses the term "function" sometimes to refer to a situation where it doesn't matter whether something is a method, a local function, or a variable bound to a function object. So, the SLS uses the term "function" both to refer to functions proper, and to refer to the more general concept of "something that can be called". This is not helping to root out the confusion either.

Upvotes: 4

Related Questions