Property rewriting; I feel it's important. Is there still time?
Andrei Alexandrescu
SeeWebsiteForEmail at erdani.org
Wed Mar 10 06:42:12 PST 2010
On 03/09/2010 09:48 PM, Chad J wrote:
> I speak of the property rewriting where an expression like
>
> foo.prop++;
>
> is rewritten as
>
> auto t = foo.prop();
> t++;
> foo.prop(t);
This particular example has a number of issues. First off you need to
rewrite expressions, not statements. Consider:
auto x = foo.prop++;
You'd need to assign to x the old value of foo.prop. So one correct
rewrite is
foo.prop++
into
{auto t = foo.prop; auto t1 = t; ++t1; foo.prop = t1; return t;}()
within an rvalue context, and into:
{auto t = foo.prop; ++t; foo.prop = t; return t;}()
within a void context.
I'm pointing out that things may not always be very simple, but
generally it's easy to figure out the proper rewrites if attention is
given to detail.
> So, Walter or Andrei or someone on the planning behind the scenes,
> please lend me your thoughts:
> How much time is left to make this sort of thing happen?
> If a working patch for this showed up, would it have a reasonable chance
> of acceptance at this point?
The idea is sensible and is already in effect for the ".length" property
of arrays.
> I really want to make this happen, even if I have to pay someone to do
> it or finish it off. It's very close but I have almost nil free time
> for this stuff.
>
> Note that I have made it work and seen it in action. There'd be a patch
> two months ago if I hadn't decided to rebel against the way DMD did things*.
Probably offering payment wouldn't be much of an enticement, but
lobbying reasonable ideas here is a good way to go.
> Now I'll try and remind you why this patch is important:
>
> - I see no other way to make properties behave like lvalues in an
> elegant manner. Explicit property syntax does not help this. This is
> quite a semantics problem, and the syntax is completely orthogonal.
Good point.
> - Having property rewrites allows the special case for "array.length +=
> foo;" to be removed. Property rewriting is the more general solution
> that will work for all properties and in arbitrary expressions.
Agreed. By the way, I'm a huge fan of lowering; I think they are great
for defining semantics in a principled way without a large language
core. In recent times Walter has increasingly relied on lowerings and
mentioned to me that the code savings in the compiler have been
considerable.
> - By treating opIndex and opIndexAssign as properties then that pair
> alone will make cases like "a[i]++;" work correctly without the need for
> opIndexUnary overloads. Also "a[i] += foo" will work too, as well as
> anything else you haven't thought of yet.
Well operator overloading handles indexing differently, and arguably
better than in your proposal. Ideally we'd define operators on
properties in a manner similar to the way indexing works in the new
operator overloading scheme. I'll talk to Walter about that.
Andrei
More information about the Digitalmars-d
mailing list