0

I'm a beginner learning Scala. I was referring to the Scala documentation.

The documentation says:

The REPL isn’t 100% the same as working with source code in an IDE, so there are a few things you can do in the REPL that you can’t do when working on real-world code in a project. One example of this is that you can redefine a val field in the REPL, like this:

scala> val age = 18
age: Int = 18

scala> val age = 19
age: Int = 19

val fields can’t be redefined like that in the real world, but they can be redefined in the REPL playground.

They have mentioned that it works differently, but what is the need for that?

Also, I have noticed that Scala is weakly-typed when I use it as Worksheet on Scastie but, strongly-typed when I turn off the worksheet mode. Why does a language behave differently in two different modes?

Edit: There was confusion. Scala works the same in no-worksheet mode.

Vraj Kotwala
  • 150
  • 8
  • 2
    _"but what is the need for that?"_ because usually a **REPL** is a place to play with the code, so assuming you are debugging an algorithm it would be way tedious if you can not replace a `val` with a new value. - _" I have noticed that Scala is weakly-typed when I use it as Worksheet on Scastie but, strongly-typed when I turn off the worksheet mode"_ **Scala** is always strongly-typed. However, you may be confused due to type-inference or due to the iterative nature of a worksheet, if you could show an example it may be easer to explain what happened and why it looked like it was weealy-typed – Luis Miguel Mejía Suárez Dec 04 '20 at 20:11
  • @LuisMiguelMejíaSuárez I tried this in worksheet mode: `var temp = "hello"` `temp = temp + 10` `print(temp)` Output was: `hello10` – Vraj Kotwala Dec 04 '20 at 20:20
  • 3
    That is not weakly-typed perse, that is the infamous `any2str` **implicit conversion** that the language has by default in the `Predef`. That is just a consequence of the **Java** origins of the language. This also works in a normal / compiled _(no-worksheet)_ mode... just try not to use it, for building strings it is better to use the simple **String interpolator**. – Luis Miguel Mejía Suárez Dec 04 '20 at 20:24
  • 2
    Please see https://stackoverflow.com/a/64667712/2359227 – Tomer Shetah Dec 04 '20 at 22:01

0 Answers0