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?
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.
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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With