Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala vals can be modified

Tags:

scala

I'm starting to play with Scala, and one of the first things I read is that vals are:

variables that are assigned once and never change, and vars, variables that may change over their lifetime

But I'm curious why I can do this:

val foo = Array(1, 3 ,2)
scala.util.Sorting.quickSort(foo)

If I check the foo variable now is ordered, which means it has changed... also if I do print(foo), both have the same, so the variable is pointing to the same object (I could have thought that the variable just pointed to a new object)

Could anyone clarify?

like image 545
jasalguero Avatar asked Jan 06 '12 16:01

jasalguero


1 Answers

The Array pointed to by the foo variable is changing, but the fact that foo points at that Array doesn't change. Try re-assigning foo and you will see what you are looking for.

like image 95
cdeszaq Avatar answered Feb 16 '23 07:02

cdeszaq