def or val for defining Function in Scala

Emmanuel

I'm learning Programming Paradigms in my University and reading this course material provided by the lecturer that defined a function this way:

val double = (x: Int) => 2 * x
double: Int => Int = <function1>

But from my own studies I found and got used to defining the same function like this:

def d (x: Int) = 2 * x
d: (x: Int)Int

I'm new to Scala. And both definitions give a result of:

res21: Int = 8

Upon passing 4 as the parameter. Now my main question is why would the lecturer prefer to use val to define a function? I see it as longer and not really necessary unless using val gives some added advantages that I don't know of. Besides I understand using val makes some name a placeholder so later in the program, I could mistakenly write val double = 5 and the function would be gone! At this stage I'm quite convinced I learned a better way of defining a function unless someone would tell me otherwise.

theon

Strictly speaking def d (x: Int) = 2 * x is a method, not a Function, however scala can transparently convert (lift) methods into Functions for us. So that means you can use the d method anywhere that requires a Int => Int Function.

There is a small overhead of performing this conversion, as a new Function instance is created every time. We can see this happening here:

val double = (x: Int) => 2 * x
def d (x: Int) = 2 * x

def printFunc(f: Int => Int) = println(f.hashCode())

printFunc(double)
printFunc(double)
printFunc(d)
printFunc(d)

Which results in output like so:

1477986427
1477986427
574533740
1102091268

You can see when explicitly defining a Function using a val, our program only creates a single Function and reuses it when we pass as an argument to printFunc (we see the same hash code). When we use a def, the conversion to a Function happens every time we pass it to printFunc and we create several instances of the Function with different hash codes. Try it

That said, the performance overhead is small and often doesn't make any real difference to our program, so defs are often used to define Functions as many people find them more concise and easier to read.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

def or val for defining Function in Scala

From Dev

Scala: overriding def with val

From Dev

val and def in Scala

From Dev

Defining function using val

From Dev

Defining variables in scala using def

From Dev

Scala trait: val/def and require

From Dev

Function Literal referencing by val and def

From Dev

Higher Order Function with `Val` and `Def`

From Dev

Scala overriding def with val throws NPE

From Java

When to use val or def in Scala traits?

From Dev

Scala overriding def with val throws NPE

From Dev

Scala val syntax: What does val myVal:{ def ... } mean?

From Dev

Defining a val in terms of the member of an abstract val (Scala bug?)

From Java

What is the difference between "def" and "val" to define a function

From Java

Why are `def aPlusOne = a + 1` and `val aPlusOne = () => a + 1` different in Scala?

From Dev

Scala: val with volatile type overriding def with non-volatile type

From Dev

How to obtain implicit val/def via parameterized types in scala?

From Dev

Scala: val with volatile type overriding def with non-volatile type

From Dev

Def vs. val, functional syntax not working in Scala.js?

From Dev

In Scala,why def not take effect after re-defining the variable used in the def expression

From Dev

what's the difference between def and var/val for an anonymous function

From Dev

Strange implicit def with function parameter behaviour in Scala

From Dev

Declaring val in scala object different to in function

From Dev

Scala: "override protected val" results in error when defining case class constructor

From Dev

def or val or lazy val for grammar rules?

From Dev

The ruby def << syntax for defining methods

From Dev

Scala - "not found: value" when calling a function before defining it

From Dev

Call a Scala "val function" from Java gives error

From Dev

Practical difference between def f(x: Int) = x+1 and val f = (x: Int) => x+1 in Scala

Related Related

  1. 1

    def or val for defining Function in Scala

  2. 2

    Scala: overriding def with val

  3. 3

    val and def in Scala

  4. 4

    Defining function using val

  5. 5

    Defining variables in scala using def

  6. 6

    Scala trait: val/def and require

  7. 7

    Function Literal referencing by val and def

  8. 8

    Higher Order Function with `Val` and `Def`

  9. 9

    Scala overriding def with val throws NPE

  10. 10

    When to use val or def in Scala traits?

  11. 11

    Scala overriding def with val throws NPE

  12. 12

    Scala val syntax: What does val myVal:{ def ... } mean?

  13. 13

    Defining a val in terms of the member of an abstract val (Scala bug?)

  14. 14

    What is the difference between "def" and "val" to define a function

  15. 15

    Why are `def aPlusOne = a + 1` and `val aPlusOne = () => a + 1` different in Scala?

  16. 16

    Scala: val with volatile type overriding def with non-volatile type

  17. 17

    How to obtain implicit val/def via parameterized types in scala?

  18. 18

    Scala: val with volatile type overriding def with non-volatile type

  19. 19

    Def vs. val, functional syntax not working in Scala.js?

  20. 20

    In Scala,why def not take effect after re-defining the variable used in the def expression

  21. 21

    what's the difference between def and var/val for an anonymous function

  22. 22

    Strange implicit def with function parameter behaviour in Scala

  23. 23

    Declaring val in scala object different to in function

  24. 24

    Scala: "override protected val" results in error when defining case class constructor

  25. 25

    def or val or lazy val for grammar rules?

  26. 26

    The ruby def << syntax for defining methods

  27. 27

    Scala - "not found: value" when calling a function before defining it

  28. 28

    Call a Scala "val function" from Java gives error

  29. 29

    Practical difference between def f(x: Int) = x+1 and val f = (x: Int) => x+1 in Scala

HotTag

Archive