std.experimental.collection.functional.slist

Timon Gehr via Digitalmars-d digitalmars-d at puremagic.com
Fri Jun 19 14:10:27 PDT 2015


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 at f35b47c, C at 741d171e, 
C at 688a3052, C at 43529d91, C at 4b564e68, C at 21d8ee20, C at 165f3028, C at 64e6bd1e, 
C at 486a8d1c, C at 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.


More information about the Digitalmars-d mailing list