On Friday, 19 June 2015 at 02:24:35 UTC, Andrei Alexandrescu wrote:
On 6/18/15 6:26 PM, ZombineDev wrote:
On Friday, 19 June 2015 at 01:07:10 UTC, ZombineDev wrote:
On Thursday, 18 June 2015 at 23:32:03 UTC, Andrei Alexandrescu wrote:
[..]

Yah, traditionally functional data structures don't allow their clients to mutate the data AND the topology of the container. So front() returns a non-mutable ref.

It might be interesting to explore containers that allow mutation of data but not of topology. We may collectively think of a few applications of that. For now I went the traditional route.


Andrei

I just realized that my idea of something in between collections and ranges obviously comes from D's own slices/dynamic arrays :D (I really shouldn't be allowed to talk so late in the night :D)

So here's an idea - can we make the new functional SList (and probably DList) the logic counter-part of slices?

const SList!int; // <- `const int[]` You can't mutate the elements,
                    // nor the topology

SList!(const(int)); // <- `const(int)[]` You can mutate the topology,
                    //but not the elements

ConstSList!(int); // <- `missing head const array` You can mutate the elements,
                    // but not the topology

SList!(int);        // <- `int[]` Unlimited power

SList!(int) mutable_list;
mutable_list ~= 3; //change the topology in-place

const SList!(int) const_list;
const_list ~= 42; //error - can't mutate const SList
const SList!(int) concat_result = const_list ~ 42; //functional-style "modification" foreach (elem; concat_result) {..} //error need mutable range, because of popFront()

SList!(const(int)) range = concat_result; // implicitly convertable // like const(int)[] <- const(int[])
foreach (elem; range) {..} // works


So instead of limiting the user by making `front` return const refs, we allow him to choose.

Reply via email to