www.digitalmars.com         C & C++   DMDScript  

D - returning lvalues?

reply Norbert Nemec <Norbert.Nemec gmx.de> writes:
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
next sibling parent reply J Anderson <REMOVEanderson badmama.com.au> writes:
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
  

-- -Anderson: http://badmama.com.au/~anderson/
Apr 24 2004
parent "Ivan Senji" <ivan.senji public.srce.hr> writes:
"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


like to do something similar in D:

Ciao,
Nobbi


Pointer has to be dereferenced == more complex code!
 --
 -Anderson: http://badmama.com.au/~anderson/

Apr 24 2004
prev sibling next sibling parent "Ivan Senji" <ivan.senji public.srce.hr> writes:
"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

 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

Apr 24 2004
prev sibling parent reply "Vathix" <vathix dprogramming.com> writes:
From: "Norbert Nemec" <Norbert.Nemec gmx.de>

 Hi there,

 in C++, it is very common to return references to allow assignment. I

 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
Apr 23 2004
parent reply Norbert Nemec <Norbert.Nemec gmx.de> writes:
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
parent "Vathix" <vathix dprogramming.com> writes:
"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; :)
Apr 23 2004