RAII, value classes and implicit conversion

Boris Kolar boris.kolar at globera.com
Wed Nov 15 06:05:33 PST 2006


== Quote from Chad J (gamerChad at _spamIsBad_gmail.com)'s article
> 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.

You're right, but it seems to me that your example is more of a hypothetical
problem than a practical one. Not sold on reason #1 ;)

> Reason #2:
> No op*Assign.  This is why we have to type
> array.length = array.length + 1;
> instead of
> array.length++;

That convinced me. I remember now that I have stumbled on this problem
as well. Still, a smart compiler might allow 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?!?

That's a good one :) Never thought of that. Although it's merely a bad
style - I always assume there are already infinite number of ways to
write program in a cryptic style.

> 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.

Well, I could never decide if a something is just a parameterless
function, a property, or a field. In D, it's all used the same way.

You can, for example, build an early prototype by using a field and
then turn it into a property (that, for example, logs every call).

Also, I like the fact that writing the parameterless function is
exactly as verbose (not less, as in C#) than writing a read-only
property.

Also, Eiffel and Wikipedia make some convincing arguments:
http://www.eiffel.com/general/monthly_column/2005/Sept_October.html
http://en.wikipedia.org/wiki/Uniform_access_principle



More information about the Digitalmars-d mailing list