On 10/16/14, 4:13 PM, Michael Dietrich wrote:
> Hello,
>
> I'm getting started with X10 and am a bit stuck with the handling of
> Rails (I think I might get the same problem with Arrays and so on).
>
> Let's say I create the following:
>
>           val bla = new Rail[Long](10);
>           bla(0) = 2343;
>           bla(1) = 4534;
>           bla(2) = 3424;
>           for(i in 0..9)
>               bla(i)=42;
>           Console.OUT.print("Content: ");
>           Console.OUT.println(bla);
>
> I get 42 ten times as an output.
> My first question: At least the first three values of my Rail are set.
> As far as I understood "val" makes them become immutable. So why can I
> turn them all into 42?
Marco explained this in a later message. The first line says that the 
variable bla is immutable, not that the members of the Rail are 
immutable. (In X10 you cannot create an array or rail whose members are 
immutable.)
> My second question: If I change the for-loop into
>
>           for(b in bla)
>               b=42;
>
> I get the expected error message by the compiler though it's actually
> no formal change. Or is there any?
>
>
The for loop will iterate through all the values in bla. In each 
iteration it will assign the immutable variable b to the value. Thus 
b=42 will generate a compile time error because you are re-assigning a 
formal variable.

What are you trying to do?

X10 is not a functional language -- some ("var") variable can be 
mutated. Hence you must be careful to keep track of which variable is 
mutable and which is not. As a rule, variables or fields introduced with 
var (and only those introduced with var) are mutable. Arrays and Rails 
are special in that their fields are indexed by longs (rather than 
names), and you do not get to specify whether the field is var or val. 
(We used to have ValArrays a long time ago but eventually got rid of 
them because they caused more confusion than they were worth.)

------------------------------------------------------------------------------
Comprehensive Server Monitoring with Site24x7.
Monitor 10 servers for $9/Month.
Get alerted through email, SMS, voice calls or mobile push notifications.
Take corrective actions from your mobile device.
http://p.sf.net/sfu/Zoho
_______________________________________________
X10-users mailing list
X10-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/x10-users

Reply via email to