pass-by-ref semantics for structs (was Deque impl.)
David Nadlinger
see at klickverbot.at
Thu Jan 31 12:08:30 PST 2013
On Thursday, 31 January 2013 at 19:55:01 UTC, Robert burner
Schadek wrote:
> Just use a payload struct and check whether that is null before
> every operation.
The following problem is inherent to this approach:
---
struct Array(T) {
static struct Payload {
T* data;
size_t length;
}
Payload* p;
// ...
}
void fill(T)(Array!T a) {
a.length = 3;
a[0] = 1;
a[1] = 2;
a[2] = 3;
}
void client() {
Array!int a;
fill(a);
// Oops, a is still empty (with null payload).
}
---
Such containers are "almost, but not quite" reference types;
reason enough to @disable this() in order to improve the user
experience, in my opinion.
David
More information about the Digitalmars-d
mailing list