On 01/31/2013 01:00 PM, monarch_dodra wrote:
On Thursday, 31 January 2013 at 11:35:31 UTC, d coder wrote:
On Thu, Jan 31, 2013 at 2:43 PM, Robert burner Schadek
<[email protected]> wrote:
Thats not totally correct. The Rb tree is a class. But the real
reason for
the Deque being a class
is that it holds two value members. And if you pass the Deque around
you
need to do this as ref.
Otherwise you shall not call any method that modifies this value
members,
because this will only be
visible at the calling site.
I would expect all the containers in the std.container to have either
pass by value or pass by reference semantics. It seems at least Array,
Slist, and Dlist are structs and they follow pass by value semantics.
From Dlang.org:
auto a = DList!int([3, 4]); //Create a new chain
auto b = a; //Point to the same chain
// (3 - 4)
assert(a[].equal([3, 4]));
assert(b[].equal([3, 4]));
b.stableInsertFront(1); //insert before of b
b.stableInsertBack(5); //insert after of b
// (2 - (3 - 4) - 5)
assert(a[].equal([3, 4])); //a is not changed
assert(b[].equal([1, 3, 4, 5])); // but b is changed
Regards
- Puneet
Technically, all containers in std.container need to adhere to
reference semantics when passed by value, regardless of if they are
classes or structs.
This is how Deque behaves. I could make it a struct and than keep the
value members on the heap, but I think there is nothing gained by that.
For example, AA's can be considered structs, but when passed, they
still adhere to reference semantics.
---------
In regards to DList: I wrote that code and documentation.
The truth is that DList was written with absolutely 0 concern to what
might happen if you mutate a copy. While correcting the code, I
preserved the old semantic (accidental) semantic, which was kind of
weird.
The mistake was to document said semantic. It makes no sense. I should
never even have preserved that semantic in the first place anyways.
There is now a new version in the tubes for Dlist, to make it behave
the way you'd expect it to:
https://github.com/D-Programming-Language/phobos/pull/953
The pull is kind of stuck in limbo, specifically because of the
problems associated with implementing reference semantics with structs :/
I read the comments to that pull request and really liked the idea of
the std.container2 by jmdavis. I would be really interested to help with
the design and impl of this
module. Do any further plans exists on that?