« Return to Thread: Reassignment to Val

Re: Reassignment to Val

by Daniel Sobral :: Rate this Message:

Reply to Author | View in Thread

If it is a function parameter, then the main's botin is no longer visible. When you "change" botin, you are trying to change the function parameter, which is not possible (no workaround either).
 
You should use the full path when trying to change botin:
 
scala> object a {
     | var x = 0;
     | def f(x: Int) = { a.x = x }
     | }
defined module a
scala> a.x
res18: Int = 0
scala> a.f(5)
scala> a.x
res20: Int = 5
 
See how I access a's x inside f?
 
By the way, by changing the a "global" variable from inside a function you are doing the worst possible sin known to functional programming. Scala allows you to do that, but I advise you to try to change that.

On Mon, Jul 6, 2009 at 7:38 PM, Joob <travaildereligion@...> wrote:

Well,

I declared the list as a "var" in my main

However it is a function parameter where I have the error

What do I have to do to make sure it stays a "var" and not a "val" ?
(If that is what I have to do, maybe I'm misunderstanding)



Randall Schulz wrote:
>
> On Monday July 6 2009, Joob wrote:
>> Hello everyone,
>>
>> I'm new to scala so please don't take anything for granted with me :)
>
> Well, you see, computers are these electronical machines that we use to
> make like difficult for others...
>
>
>> Anyway,
>> I'm getting this error "Reassignment to val"
>
> A "val" is an "immutable" record of a value. By immutable we mean it
> cannot be reassigned. There are lots of positive virtues to defining
> programs in terms of immutable data, but it's sometimes challenging to
> do so. Unlike Haskell, which is considered a "pure" functional language
> that strictly forbids such "side-effects" and "mutable state," Scala
> doesn't force you to use immutable data. When you feel the need to be
> able to reassign the value in a field or local variable (or function /
> method parameter), you can use a "var" instead of a val.
>
>
>> ...
>>
>> Can anyone help me ?
>
> Anything could happen...
>
>
> Randall Schulz
>
>

--
View this message in context: http://www.nabble.com/Reassignment-to-Val-tp24363871p24364035.html
Sent from the Scala - User mailing list archive at Nabble.com.




--
Daniel C. Sobral

Something I learned in academia: there are three kinds of academic reviews: review by name, review by reference and review by value.

 « Return to Thread: Reassignment to Val