RAII, value classes and implicit conversion
Chad J
gamerChad at _spamIsBad_gmail.com
Wed Nov 15 03:04:29 PST 2006
Boris Kolar wrote:
> == Quote from Chris Nicholson-Sauls (ibisbasenji at gmail.com)'s article
>
>>Also, it makes me think of C# style properties -- and while I would like to see
>
> them in D,
>
>>I don't need constant false hope. ;)
>
>
> I like D-style properties much better :)
>
Myself I really dislike D-style properties (as opposed to C# style).
Reason #1: delegate ambiguity
class Foo
{
void delegate() m_bar;
void delegate() bar() { return m_bar; }
void func()
{
writefln( "..." );
}
this()
{
m_bar = &func;
}
}
void main()
{
Foo foo = new Foo();
foo.bar(); // call foo.bar, or return the delegate m_bar?
}
If you think of properties as member variables, then this does not
behave as expected - foo.bar() returns the delegate, but does not call
it. Thinking of foo.bar as a method that returns a delegate does cause
the behaviour make sense, but then it's just shorthand for calling a
method, and nothing more.
Reason #2:
No op*Assign. This is why we have to type
array.length = array.length + 1;
instead of
array.length++;
Reason #3:
It's one more possible way to write code that just doesn't make sense.
I suppose this isn't too bad since programmers tend to shoot for
clarity, but I've run into it.
writefln = sqrt = 4.0f; // what?!?
...
So I see the current property syntax as nothing more than a shorthand
for function/method calling. It does not provide a means to wrap code
around the getting and setting of member variables or have member
variables with inheritance properties, because properties are not member
variables they are methods. I am a fast typer - I don't mind the extra
2 parens to call a function. However, being a fast typer does not solve
the delegate ambiguity (a bit of thought can, but that's what properties
are supposed to save IMO).
Also I used C# properties a lot and enjoyed them thoroughly. No
problems there.
Well that's my case. Could you explain why you prefer D-style
properties, please? I'd like to know if there are any significant
advantages to having it this way.
More information about the Digitalmars-d
mailing list