Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why use def and val in Scala or vice versa

Tags:

scala

I know this has been discussed on SO in other posts before and I understand the basic difference between the use of def and val. def is used for defining a method and val for an immutable reference. What I am trying to accomplish by asking this question is to understand if there is something more to def. Can it be used interchangeably with a val?

Recently I tried the following code and cannot convince myself if my present understanding of def is sufficient:

scala> def i: Int = 3
i: Int

scala> i
res2: Int = 3

So I am curious, is this equivalent to val i = 3?

Then I tried this:

scala> i()
<console>:9: error: Int does not take parameters
i()

I did this just to test my understanding of the semantics of def. Now I want to know, when i is a method, why Scala complains with "...does not take parameters"?

Next I tried the following:

scala> def i(): Int = 3
i: ()Int

scala> i()
res4: Int = 3

This time Scala seems to agree that i is a method. So can I use def in place of val interchangeable to declare and initialize a variable?

like image 235
ilango Avatar asked Oct 08 '12 18:10

ilango


People also ask

Why do we use val in Scala?

In Scala the general rule is that you should always use a val field unless there's a good reason not to. This simple rule (a) makes your code more like algebra and (b) helps get you started down the path to functional programming, where all fields are immutable.

What is the difference between a VAR Def and val in Scala?

In short, the val and var are evaluated when defined, while def is evaluated on call. Also, val defines a constant, a fixed value that cannot be modified once declared and assigned while var defines a variable, which can be modified or reassigned.

What does def do in Scala?

def is the keyword you use to define a method, the method name is double , and the input parameter a has the type Int , which is Scala's integer data type.

Is it OK to use VAR in Scala?

Performance: Sometimes using a var gives you the best possible performance. When people say that everything can be done without vars, that is correct in the sense that Scala would still be turing complete without vars. However, it doesn't change anything about the validity of the previous points.


2 Answers

Both

def i = 3

and

def i() = 3

declare methods. The only difference is, that the first one is a method without a parameter list and the second is a method with an empty parameter list. The former is usually used for methods without side effects and the latter for methods with side effects. You should use a val instead of a def if the value never changes and you want to avoid recomputing it. A def gets recomputed every time it is called, while a val is assigned a value only once.

like image 70
Kim Stebel Avatar answered Sep 30 '22 17:09

Kim Stebel


def defines a method, val defines an immutable value, as you already know.

One major difference is in when the expression on the right side of the = is evaluated. For a method, it is evaluated each time you call the method. For a value, it is evaluated when you initialize the value. See the difference:

scala> def i: Int = { println("Hello"); 3 }
i: Int

scala> i
Hello
res0: Int = 3

scala> i
Hello
res1: Int = 3

scala> val i: Int = { println("Hello"); 3 }
Hello
i: Int = 3

scala> i
res2: Int = 3
like image 20
Jesper Avatar answered Sep 30 '22 18:09

Jesper