-1

In REPL when we type the below command

scala> val p = 1 << 1
p: Int = 2

again

scala> val p = 1 << 2
p: Int = 4

my question is , I read that val is immutable . but in this case the value is changing right . Well can someone tell me why . is this really an example of mutatating . Please help

Johny T Koshy
  • 3,857
  • 2
  • 23
  • 40
StandForTheRight
  • 135
  • 3
  • 14

2 Answers2

6

This behaviour appears in REPL only. If you try to define val twice in Scala code you'll get compilation error. In REPL second definition of val just shadows previous value of p

Nyavro
  • 8,806
  • 2
  • 26
  • 33
0

yep, as nyavro said, in the REPL you can override vals. Just think that in IDE if you make a mistake typing a value you can fix, in the REPL how would you fix? you would need to close the session?

Community
  • 1
  • 1
pedrorijo91
  • 7,635
  • 9
  • 44
  • 82