The Artima Developer Community
Sponsored Link

Programming in Scala Forum
val vs var

6 replies on 1 page. Most recent reply: Nov 30, 2012 6:08 PM by philippos papadatos

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 6 replies on 1 page
Jose Galeano

Posts: 7
Nickname: galeanoj
Registered: Nov, 2012

val vs var Posted: Nov 2, 2012 4:44 PM
Reply to this message Reply
Advertisement
The book speak about val variables could not be changed. I do this commands, and run ok, what is the true then ????

Seems like in certain condition we can change the val variables Too! little by little....is a language fail?!

Sequence of commands that I type....
JVM 1.5.0_22

scala> val msg3: String = "Hello yet again, world!"
msg3: String = Hello yet again, world!

scala> val msg3 = "Hello yet again, world"
msg3: java.lang.String = Hello yet again, world

scala> val msg3 = "Hello yet again, worl"
msg3: java.lang.String = Hello yet again, worl

scala> val msg3 = "Hello yet again, woral"
msg3: java.lang.String = Hello yet again, woral

scala> val msg3 = "Hello yet again, bye"
msg3: java.lang.String = Hello yet again, bye


Jose Galeano

Posts: 7
Nickname: galeanoj
Registered: Nov, 2012

Re: val vs var Posted: Nov 2, 2012 4:49 PM
Reply to this message Reply
Could be that the definition of msg3 is overwrited each time I use val in the sentence again and again....

val msg3 ="one"
val msg3 ="two"

and using always val as first part of the sentence msg3 can be changed always.....

George Berger

Posts: 21
Nickname: gcb
Registered: Jul, 2007

Re: val vs var Posted: Nov 2, 2012 4:58 PM
Reply to this message Reply
From section 7.7 of Programming in Scala (http://www.artima.com/pins1ed/builtin-control-structures.html):

You might have already noticed something that looks like shadowing in the interpreter:

scala> val a = 1
a: Int = 1

scala> val a = 2
a: Int = 2

scala> println(a)
2

In the interpreter, you can reuse variable names to your heart's content. Among other things, this allows you to change your mind if you made a mistake when you defined a variable the first time in the interpreter. The reason you can do this is that, conceptually, the interpreter creates a new nested scope for each new statement you type in. Thus, you could visualize the previous interpreted code like this:

val a = 1;
{
val a = 2;
{
println(a)
}
}

Jose Galeano

Posts: 7
Nickname: galeanoj
Registered: Nov, 2012

Re: val vs var Posted: Nov 5, 2012 3:45 PM
Reply to this message Reply
Thanks, you are speaking about scope.
I test it in a While statement, and yes return error..in the same scope..

scala> while ( i < 3) {
| val num2 = 10
| print (num2)
| val num2 = 45
| i += 1;
| }
<console>:10: error: num2 is already defined as value num2
val num2 = 45
^
and this other case a first definition of num1 and a change in the while statement...

scala> val num1 = 10
scala> while ( i < 3) {
| print (num1)
| val num1 = 30
| i += 1;
| }
<console>:9: error: forward reference extends over definition of value num1
print (num1)
^
but the interpreter can overwrite the definitions if they are done only in the command line....

Jose Galeano

Posts: 7
Nickname: galeanoj
Registered: Nov, 2012

Re: val vs var Posted: Nov 5, 2012 3:53 PM
Reply to this message Reply
but in the second case above, the "While" is returning error and should not... "if I follow the logic of your answer"...The sentence "while" Is in another scope? or not ??? Why the "while" return error in the second example then....??? that is a prove that the while is in the same scope or can not to overwrite the first definition seems like the first definition in the command line is a global definition....is confuse for me...yet....

Jose Galeano

Posts: 7
Nickname: galeanoj
Registered: Nov, 2012

Re: val vs var Posted: Nov 5, 2012 4:09 PM
Reply to this message Reply
I had a mistake the second error is another type of error

scala> print (i)
3
scala> i = 0
i: Int = 0

scala> while ( i < 3) {
| val num1 = 40
| print (num1)
| i += 1;
| }
404040
scala>

I was typing "print (num1) " before the definition "val num1 ".
The num1 from the "while", is not he same as the num1 definition in the command line..
all is ok for me now, thanks!

philippos papadatos

Posts: 6
Nickname: firesoft
Registered: Mar, 2012

Re: val vs var Posted: Nov 30, 2012 6:08 PM
Reply to this message Reply
The Scala REPL is meant for a quick test of a few lines it is not meant to be used as an IDE.

Download the Typesafe's eclipse IDE and play around there http://typesafe.com/stack/scala_ide_download

val num1 = 10
val num1 = 11
will produce a compile time error... because "val" declares an immutable value, "var" on the other hand declares a mutable value i.e variable

Flat View: This topic has 6 replies on 1 page
Topic: Paper book order status Previous Topic   Next Topic Topic: thrill.foreach(s => print(s))


Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2014 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us