D - returning lvalues?
- Norbert Nemec (18/18) Apr 24 2004 Hi there,
- J Anderson (4/10) Apr 24 2004 Use a pointer.
- Ivan Senji (4/17) Apr 24 2004 would
- Ivan Senji (9/27) Apr 24 2004 would
- Vathix (6/24) Apr 23 2004 Why not use opIndex? That's what it's there for...
- Norbert Nemec (4/6) Apr 24 2004 a) It is limited to just one index, so it will fail for multidimensional
- Vathix (5/11) Apr 23 2004 I understand. If there were array literals, the index type of opIndex co...
Hi there, in C++, it is very common to return references to allow assignment. I would like to do something similar in D: -------------------------- class Vector(T) { T[] data; this(int size) { data.length = size; } T& operator(int idx) { return data[idx]; }; }; int main() { Vector!(int) a(3); a(2) = 5; }; -------------------------- However, that T& is not defined in D. What do I do? Ciao, Nobbi
Apr 24 2004
Norbert Nemec wrote:Hi there, in C++, it is very common to return references to allow assignment. I would like to do something similar in D: Ciao, NobbiUse a pointer. -- -Anderson: http://badmama.com.au/~anderson/
Apr 24 2004
"J Anderson" <REMOVEanderson badmama.com.au> wrote in message news:c6dcii$28jj$2 digitaldaemon.com...Norbert Nemec wrote:wouldHi there, in C++, it is very common to return references to allow assignment. IPointer has to be dereferenced == more complex code!like to do something similar in D: Ciao, NobbiUse a pointer.-- -Anderson: http://badmama.com.au/~anderson/
Apr 24 2004
"Norbert Nemec" <Norbert.Nemec gmx.de> wrote in message news:c6db59$24t0$1 digitaldaemon.com...Hi there, in C++, it is very common to return references to allow assignment. Iwouldlike to do something similar in D:I would like to be able to do that too! This way there are inconsistenties between user defined types (like class) and non-reference types. This could be a problem in templates and generic algorithms that should work both for builtin types and for user defined types the same way :(-------------------------- class Vector(T) { T[] data; this(int size) { data.length = size; } T& operator(int idx) { return data[idx]; }; }; int main() { Vector!(int) a(3); a(2) = 5; }; -------------------------- However, that T& is not defined in D. What do I do? Ciao, Nobbi
Apr 24 2004
From: "Norbert Nemec" <Norbert.Nemec gmx.de>Hi there, in C++, it is very common to return references to allow assignment. Iwouldlike to do something similar in D: -------------------------- class Vector(T) { T[] data; this(int size) { data.length = size; } T& operator(int idx) { return data[idx]; }; }; int main() { Vector!(int) a(3); a(2) = 5; }; -------------------------- However, that T& is not defined in D. What do I do? Ciao, NobbiWhy not use opIndex? That's what it's there for... Type1 opIndex(Type2 index, Type1 value); -- Christopher E. Miller
Apr 23 2004
Vathix wrote:Why not use opIndex? That's what it's there for... Type1 opIndex(Type2 index, Type1 value);a) It is limited to just one index, so it will fail for multidimensional arrays b) it can only be used for assignments but not for inout parameters etc.
Apr 24 2004
"Norbert Nemec" <Norbert.Nemec gmx.de> wrote in message news:c6ddf1$2b63$2 digitaldaemon.com...Vathix wrote:I understand. If there were array literals, the index type of opIndex could be int[], and you could specify like: arr[[3, 4, 5]] = 4; :)Why not use opIndex? That's what it's there for... Type1 opIndex(Type2 index, Type1 value);a) It is limited to just one index, so it will fail for multidimensional arrays b) it can only be used for assignments but not for inout parameters etc.
Apr 23 2004