On 06/19/2015 03:21 PM, Andrei Alexandrescu wrote:
On 6/19/15 2:28 AM, Timon Gehr wrote:
That's not the issue. The data held by the structure shouldn't be
mutated, since it is persistent. Returning const(T) means that the data
that the return value refers to may not be mutated. This is crippling.

I don't understand. So what should be done here?

What Scala does.

scala> class C { var x:Int=0 }
defined class C

scala> val s = (Range(0,10) map { _ => new C }).toSet
s: scala.collection.immutable.Set[C] = Set(C@f35b47c, C@741d171e, C@688a3052, C@43529d91, C@4b564e68, C@21d8ee20, C@165f3028, C@64e6bd1e, C@486a8d1c, C@28f9883c)

scala> for(x <- s) print(x.x)
0000000000
scala> for(x <- s) x.x += 1

scala> for(x <- s) print(x.x)
1111111111


Note that I cannot change the set in-place.

The memory allocated by the persistent data structure should not be exposed, but if mutable references are stored in the structure, it should be possible to mutate the data those references refer to after the references have been obtained from (rvalue) front.

If the stored data is to be further constrained, we do have type modifiers to signify it, but it is not the library's job to constrain the design space artificially.

Reply via email to