Property rewriting; I feel it's important. Is there still time?

retard re at tard.com.invalid
Wed Mar 10 02:05:03 PST 2010


Tue, 09 Mar 2010 22:48:08 -0500, 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);
> 
> 
> 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?
> 
> 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*.
> 
> 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.
> 
> - 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.
> 
> - 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.

This is so unbelievable. I knew the property stuff was being redesigned 
since there was so much talk in the ng some time ago. But even now, why 
on earth doesn't it work like it should. Is it so hard to copy/steal the 
good ideas from the better languages. Guess how C#/Scala solved this - I 
bet having a PhD helps getting things right the first time..

Scala:
------

object foo {
  def a = { println("getter returns 42"); 42 }
  def a_=(b:Int) = println("setter sets " + b)
}

scala> foo.a += 3
getter returns 42
setter sets 45


C#:
---

using System;

class Hello {
  public int id
  {
    get
    {
      Console.WriteLine ("getter returns 42");
      return 42;
    }
    set
    {
      Console.WriteLine ("setter sets " + value);
    }
  }

  static void Main() {
    Hello h = new Hello();
    h.id += 2;
  }
}


D:
--

struct Foo
{
    @property int data() { return m_data; }     // read property

    @property int data(int value) { return m_data = value; } // write 
property

  private:
    int m_data;
}

void main() {
  Foo f;
  f.data += 3;
}

test.d(13): Error: 'f.data' is not a scalar, it is a @property int()
test.d(13): Error: incompatible types for ((f.data) += (3)): '@property 
int()' and 'int'



More information about the Digitalmars-d mailing list