Why do struct literals count as lvalues?
Jonathan M Davis
jmdavisProg at gmx.com
Thu Aug 18 16:51:04 PDT 2011
On Thursday, August 18, 2011 15:56 bearophile wrote:
> Jonathan M Davis:
> > I see _zero_ reason to
> > treat a newly constructed object as any different from one which is
> > returned from a function.
>
> I have not followed fully this thread, so I am not sure what you are
> talking about, so sorry if I am misunderstanding the whole topic.
>
> I assume you want to disallow code like:
>
> struct Vect {
> float[4] data = 0.0;
> // lot of operator overloading here, all arguments are by ref
> }
> void foo(ref Vect f) {}
> void main() {
> foo(Vect([1,2,3,4]));
> }
>
> Such code is common. In foo() and in all the Vect operators ref is required
> for performance. This code is quite handy. Forcing me to define and assign
> a Vect to a temporary variable in main is not handy.
>
> Think about writing expressions that use Vects with operator overloading,
> that use ref everywhere. If you can't pass and give Vects defined inside
> the expressions the code becomes quite more hairy.
That may be, but it doesn't refer to an actual variable, so ref makes no sense
IMHO, and regardless, the return value of a function is just as hairy. So,
with the current situation
foo(Vect([1, 2, 3, 4]));
works, but
foo(func());
doesn't (assuming that func returns ' Vec). That makes no sense to me. Why
treat one differently than the other? It's just confusing.
- Jonathan M Davis
More information about the Digitalmars-d
mailing list