Hi Michael, Am 16.10.2014 um 22:13 schrieb Michael Dietrich: > 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?
The semantics are not like in C(++) (where the Array AND its entry are immutable), but only the reference to the array is immutable, like in Java. Meaning: you can modify single entries, but you cannot write bla = new Rail[Long] (...); after the first initialization. > > 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? > > bye > Michael You should wirte the loop as for (b:Long in bla) { b = 42;} which results in this compiler-error: Final variable might already have been initialized. Final variable: b Writing for (var b:Long in bla) { b = 42; } Leads to: Enhanced for loop may not have var loop index. var b: Long{amb} Final variable might already have been initialized. Final variable: b In other words: if you use primitive data types (int, long, double, ...), you will not be able to change them with a foreach-loop. Even if you were able to change the value of b, you would change ONLY the value of b, since primitives are copied and not referenced. If, on the other hand, your Rail holds objects, the loop-variable (b) will hold references. While b is still a val, you would be able to change attributes of b (just like you were able to change entries in the Rail bla despite it being a val) and since b is only a reference to the object in the Rail, you would get the desired result. I hope i was able to help. Feel free to ask further questions. Cheers, Marco > > > ------------------------------------------------------------------------------ > 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 > ------------------------------------------------------------------------------ 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