Suggestion: properties should be treated as 'virtual members variables'
Chad J
gamerChad at _spamIsBad_gmail.com
Fri Sep 8 16:53:56 PDT 2006
Just thought I'd point out another shortcoming of the current approach
to properties.
Suppose you have the following code:
class Foo
{
void delegate() bar;
void func()
{
writefln( "example" );
}
this()
{
bar = &func;
}
}
void main()
{
Foo foo = new Foo();
foo.bar();
}
This prints "example". But now the class is rewritten so that the
delegate is hidden behind a property:
class Foo
{
void delegate() m_bar;
void delegate() bar() { return m_bar; }
void func()
{
writefln( "example" );
}
this()
{
m_bar = &func;
}
}
void main()
{
Foo foo = new Foo();
foo.bar();
}
Unfortunately this still compiles and runs... incorrectly. It prints
nothing. foo.bar() just returns a delegate, but doesn't call it.
IMO, this is quite underpowered compared to what something like C# gives
you. Currently it's just shorthand for function calling, and that
hardly helps me. I don't mind typing a few extra characters to call a
function. What I want is to be able to treat properties as data members
in /every/ way on the calling side.
I hope there are plans to change the current state of properties at some
point, regardless of backwards compatibility.
Kristian wrote:
>
> In digitalmars.D.announce there has been discussion about why you
> cannot use properties as data members. For example, why the following
> (simple) example doesn't work:
>
> class A {
> int prop() {return m_val;}
> int prop(int val) {return m_val = val;}
>
> int m_val = 0;
> }
>
> void f(inout int x) {
> x++;
> }
>
> void func() {
> A a = new A;
>
> f(a.prop); //error: ((a).prop)() is not an lvalue
> }
>
>
> I think properties should work just like they were data members:
> everywhere you could give a data member you can also give a property.
> So, for example, the following should also work:
>
> a.prob++;
> a.prob -= 10;
>
> Hence the implementation of 'prob' will be hidden from the user.
>
> I would very much see properies as a 'virtual member variables'.
>
>
> Hmm, would it be simplier to implement properties by using virtual
> tables for variables?...
>
> (What object oriented languages did for functions, D does it for
> variables! *grin*)
More information about the Digitalmars-d
mailing list