opEquals(const ref yadaYada)

Leandro Lucarella llucax at gmail.com
Mon Dec 14 05:47:44 PST 2009


lws, el 14 de diciembre a las 01:06 me escribiste:
> I don't know if I believe this is necesarrily bad.  It's revealing
> some bad coding on your part.
> 
> You shouldn't be doing opEquals with an rvalue of a class.   Make
> getFoo return a reference.
> 
> ref Foo getFoo() {} fixes the problem and avoids value-copying for
> no reason to an rvalue that's going to get garbage collected.

If you are using "garbage collected" as a general term to denote
"automatic memory reclamation", you are right, but the garbage collector
have nothing to do with rvalues, they are allocated in the stack, and they
will be "garbage collected" by the compiler when unwinding the stack, not
the GC.

I don't know if this is relevant to the discussion, I'm clarifying, just
in case :)

> On 2009-12-12 07:14:50 -0800, dsimcha <dsimcha at yahoo.com> said:
> 
> >I've noticed that, for DMD 2.037, we've started mandating that the input
> >parameter for struct opEquals be const ref T.  This seemed like a good idea
> >initially, but it creates the horribly leaky abstraction that the right-hand
> >argument to opEquals can't be an rvalue.  Example:
> >
> >struct Foo {
> >    bool opEquals(const ref Foo rhs) const {  // Only signature
> >                                              // that compiles.
> >        return true;
> >    }
> >}
> >
> >Foo getFoo() {
> >    return Foo();
> >}
> >
> >void main() {
> >    Foo foo = getFoo();
> >    bool isEqual = foo == getFoo();
> >}
> >
> >Error:  Foo.opEquals type signature should be const bool(ref const(Foo)) not
> >const bool(Foo rhs)
> >
> >Will this be getting fixed witht he new operator overloading?
> 
> 

-- 
Leandro Lucarella (AKA luca)                     http://llucax.com.ar/
----------------------------------------------------------------------
GPG Key: 5F5A8D05 (F8CD F9A7 BF00 5431 4145  104C 949E BFB6 5F5A 8D05)
----------------------------------------------------------------------
I would drape myself in velvet if it were socially acceptable.
	-- George Constanza



More information about the Digitalmars-d mailing list