Val assignment

58 views
Skip to first unread message

jithesh karunakaran

unread,
Oct 28, 2016, 1:41:00 PM10/28/16
to scala-user

Hi,

 

Attached is the screenshot of my windows powershell, where I am running Scala.

I have tried assigning different values to the same Val a. which is an integer Val, I could assign different values to the Val a. As per Scala, it will not be possible to assign a different value to a Val which has already assigned a value.

Please help.

Respectfully,

Jithesh.
powershell.JPG

Oliver Ruebenacker

unread,
Oct 28, 2016, 1:58:01 PM10/28/16
to jithesh karunakaran, scala-user

     Hello,

  These are all different vals. Every time you use the val keyword, you define a new val. Reassignment would be to use without the val keyword like this:

Welcome to Scala 2.11.8 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_45).
Type in expressions for evaluation. Or try :help.

scala> val a = 1
a: Int = 1

scala> a = 2
<console>:12: error: reassignment to val
       a = 2
         ^


  The Scala Shell (aka REPL) allows you to define different things of the same name, and then the latest will shadow the earlier ones. This is an artefact of how the Scala Shell works.

     Best, Oliver

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Oliver Ruebenacker
Senior Software Engineer, Diabetes Portal, Broad Institute

Seth Tisue

unread,
Oct 28, 2016, 2:20:50 PM10/28/16
to scala-user
this shows that shadowing, not reassignment, is taking place:

scala> val a = 3

a: Int = 3


scala> def b = a

b: Int


scala> val a = 4

a: Int = 4


scala> b

res0: Int = 3


jithesh karunakaran

unread,
Oct 28, 2016, 2:25:25 PM10/28/16
to Oliver Ruebenacker, scala-user
Thank you very much Oliver.  

Oliver Ruebenacker

unread,
Oct 28, 2016, 2:45:57 PM10/28/16
to Seth Tisue, scala-user

     Hello,

  You can actually access the shadowed vals:

Welcome to Scala 2.11.8 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_45).
Type in expressions for evaluation. Or try :help.

scala> object S { var lines: Seq[AnyRef] = Seq.empty }
defined object S

scala> val a = 23; S.lines :+= this
a: Int = 23

scala> val a = 42; S.lines :+= this
a: Int = 42

scala> val a = 114; S.lines :+= this
a: Int = 114

scala> S.lines
res3: Seq[AnyRef] = List(@5ff2b8ca, @618ad2aa, @1aa6e3c0)

scala> S.lines(0).asInstanceOf[{ val a: Int }].a
warning: there was one feature warning; re-run with -feature for details
res4: Int = 23

scala> S.lines(1).asInstanceOf[{ val a: Int }].a
warning: there was one feature warning; re-run with -feature for details
res5: Int = 42

scala> S.lines(2).asInstanceOf[{ val a: Int }].a
warning: there was one feature warning; re-run with -feature for details
res6: Int = 114

     Best, Oliver

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

som-snytt

unread,
Nov 6, 2016, 1:01:56 AM11/6/16
to scala-user, cur...@gmail.com

Another demo, using //print<tab> to show the paths:

$ scala
Welcome to Scala 2.12.0 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_101).

Type in expressions for evaluation. Or try :help.

scala> val x = 42
x: Int = 42

scala> x //print
   $line3.$read.$iw.$iw.x // : Int

scala> val x = 17
x: Int = 17

scala> x //print
   $line5.$read.$iw.$iw.x // : Int

scala> ($line3.$read.$iw.$iw.x, $line5.$read.$iw.$iw.x)
res0: (Int, Int) = (42,17)


On Friday, October 28, 2016 at 11:25:25 AM UTC-7, jithesh karunakaran wrote:
Thank you very much Oliver.  
On Fri, Oct 28, 2016 at 9:57 PM, Oliver Ruebenacker <cur...@gmail.com> wrote:

     Hello,

  These are all different vals. Every time you use the val keyword, you define a new val. Reassignment would be to use without the val keyword like this:

Welcome to Scala 2.11.8 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_45).
Type in expressions for evaluation. Or try :help.

scala> val a = 1
a: Int = 1

scala> a = 2
<console>:12: error: reassignment to val
       a = 2
         ^


  The Scala Shell (aka REPL) allows you to define different things of the same name, and then the latest will shadow the earlier ones. This is an artefact of how the Scala Shell works.

     Best, Oliver
On Fri, Oct 28, 2016 at 1:31 PM, jithesh karunakaran <jithe...@gmail.com> wrote:

Hi,

 

Attached is the screenshot of my windows powershell, where I am running Scala.

I have tried assigning different values to the same Val a. which is an integer Val, I could assign different values to the Val a. As per Scala, it will not be possible to assign a different value to a Val which has already assigned a value.

Please help.

Respectfully,

Jithesh.

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages