The Artima Developer Community
Sponsored Link

Programming in Scala Forum
val similar to Java final?

3 replies on 1 page. Most recent reply: Nov 23, 2008 8:04 PM by Vladimir Kelman

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
Kean Heng Lim

Posts: 5
Nickname: 53770
Registered: Jan, 2008

val similar to Java final? Posted: May 23, 2008 8:51 AM
Reply to this message Reply
Advertisement
Hi

Some questions from beginner of Scala -

If val similar to Java final but not the same, then what is the difference, in short?

Why both val and var are needed? While a val variable can't be re-assigned but it's always can be re-defined, doesn't that has the same effect as using var?

scala> val msg="Hello"
msg: java.lang.String = Hello

scala> msg="World"
<console>:5: error: reassignment to val
msg="World"
^

scala> val msg="Hello, World"
msg: java.lang.String = Hello, World


Regards
KH


Callum Rhodes

Posts: 3
Nickname: 54709
Registered: Mar, 2008

Re: val similar to Java final? Posted: May 24, 2008 9:28 AM
Reply to this message Reply
Hi Kean

Although a val can be re-defined the origonal still exists, consider the two cases:


scala> val msg = "hello"

scala> def printMsg(): Unit = print(msg)

scala> printMsg() // Prints "hello"

scala> val msg = "hello world"

scala> printMsg() // Prints "hello" still

scala> print(msg) // This one prints "hello world"


On the other hand:


scala> var msg = "hello"

scala> def printMsg(): Unit = print(msg)

scala> printMsg() // Prints "hello"

scala> msg = "hello world"

scala> printMsg() // Now it prints "hello world"


There are a couple of other things to note, 1) you can re-define vars too, and 2) in a '.scala' file (script or otherwise) when re-defining vals/vars you must be in a different scope ie:


val msg = "hello"
{
val msg = "hello world"
}


works but


val msg = "hello"
val msg = "hello world"


doesn't.

Callum

Jeff Heon

Posts: 40
Nickname: jfheon
Registered: Feb, 2005

Re: val similar to Java final? Posted: May 26, 2008 7:27 AM
Reply to this message Reply
If my memory is correct, each statement in the interpreter is inside a nested scope, which explains that the val is not redefined at all, it is just defined in an inner scope.

Vladimir Kelman

Posts: 46
Nickname: vkelman
Registered: Feb, 2008

Re: val similar to Java final? Posted: Nov 23, 2008 8:04 PM
Reply to this message Reply
Interestingly enough, F# language uses a special #light directive and, after you applied this directive, it begins to function like Scala interpreter, making subsequent statements to define nested scopes.
In C#, on the other hand, even the following is prohibited:

int i = 5;
{
int i = 3;
}

i.e. in C# in a nested scope you still cannot define a variable with the same name as on in an outer scope.

Flat View: This topic has 3 replies on 1 page
Topic: By-name parameters Previous Topic   Next Topic Topic: Ch2:Step5 pre-post increment operators

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use