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

前端 未结 2 1427
既然无缘
既然无缘 2020-11-30 12:17
scala> val hi = \"Hello \\\"e\"
hi: String = Hello \"e


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

Within same REPL session why its allowing me

2条回答
  •  自闭症患者
    2020-11-30 12:30

    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.

提交回复
热议问题