Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is it possible to declare variable with same name in the REPL?

scala> val hi = "Hello \"e"
hi: String = Hello "e


scala> val hi = "go"
hi: String = go

Within same REPL session why its allowing me to declare variable hi with same name ?

scala> hi
res1: String = go

scala> hi="new"
<console>:8: error: reassignment to val
   hi="new"
     ^

This error i understood we cannot reassign val

like image 550
user2230605 Avatar asked Mar 31 '14 21:03

user2230605


2 Answers

The interesting design feature of the REPL is that your two definitions are translated to:

object A {
  val greeting = "hi"
}
object B {
  val greeting = "bye"
}

A subsequent usage will import the last definition:

object C {
  import B.greeting
  val message = s"$greeting, Bob."  // your code
}

You can witness the exact wrapping strategy with scala -Xprint:parser:

object $iw extends scala.AnyRef {
  def <init>() = {
    super.<init>();
    ()
  };
  import $line4.$read.$iw.$iw.greeting;
  object $iw extends scala.AnyRef {
    def <init>() = {
      super.<init>();
      ()
    };
    val message = StringContext("", ", Bob.").s(greeting)
  }
}
like image 111
som-snytt Avatar answered Oct 11 '22 13:10

som-snytt


In the first piece of code I believe this is a "feature" of the REPL allowing your to redefine hi. Suppose you are working through building a small piece of code in the REPL then it might be helpful to go back and change a prior definition without rewriting the others to use a different value.

The following code would give an error error: x is already defined as value x when compiling with scalac.

class Foo{
  val x = "foo"
  val x = "foo"
}

In the second piece of code you are trying to reassign a val which cannot be changed. This is what you would expect.

like image 28
Brian Avatar answered Oct 11 '22 15:10

Brian