Thread overview
returning lvalues?
Apr 24, 2004
Norbert Nemec
Apr 24, 2004
J Anderson
Apr 24, 2004
Ivan Senji
Apr 24, 2004
Ivan Senji
Apr 24, 2004
Vathix
Apr 24, 2004
Norbert Nemec
Apr 24, 2004
Vathix
April 24, 2004
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
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
"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
"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
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
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
"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;
:)