Thread overview | |||||||||
---|---|---|---|---|---|---|---|---|---|
|
April 24, 2004 returning lvalues? | ||||
---|---|---|---|---|
| ||||
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 |
April 24, 2004 Re: returning lvalues? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Norbert Nemec | 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, >Nobbi > > Use a pointer. -- -Anderson: http://badmama.com.au/~anderson/ |
April 24, 2004 Re: returning lvalues? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Norbert Nemec | "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. I would > like 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 |
April 24, 2004 Re: returning lvalues? | ||||
---|---|---|---|---|
| ||||
Posted in reply to J Anderson | "J Anderson" <REMOVEanderson@badmama.com.au> wrote in message news:c6dcii$28jj$2@digitaldaemon.com... > 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, > >Nobbi > > > > > Use a pointer. Pointer has to be dereferenced == more complex code! > -- > -Anderson: http://badmama.com.au/~anderson/ |
April 24, 2004 Re: returning lvalues? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Norbert Nemec | From: "Norbert Nemec" <Norbert.Nemec@gmx.de> > 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 Why not use opIndex? That's what it's there for... Type1 opIndex(Type2 index, Type1 value); -- Christopher E. Miller |
April 24, 2004 Re: returning lvalues? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Vathix | 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.
|
April 24, 2004 Re: returning lvalues? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Norbert Nemec | "Norbert Nemec" <Norbert.Nemec@gmx.de> wrote in message news:c6ddf1$2b63$2@digitaldaemon.com... > 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. 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; :) |
Copyright © 1999-2021 by the D Language Foundation