D vs. C#

Janice Caron caron800 at googlemail.com
Sun Oct 21 04:16:59 PDT 2007


On 10/21/07, Yigal Chripun <yigal100 at gmail.com> wrote:
> object.doSomething(params);
> ---
> is more OOP correct than:
> ---
> a = object.getField();
> b = doSomthing(a, params);
> object.setField(b);

I think that's wrong.

The big benefit of properties is that they are /functions/, and
functions are overridable, and provide polymorphism. Thus, if I do

    a.n = a.n + 1;

(which we hope will, in future versions of D, be allowed to be written
more simply as

    ++a.n;

) then the function a.n() is being called, followed by the funtion
a.n(int). If a is subclassed, then the subclass may override n() and
n(int), providing a different implementation. This means that, in true
OOP fashion, I can write

    abstract class A
    {
        abstract int n();
        abstract void n(int x);
    }

and thereby force all subclasses to implement the property n in a
polymorphic way.

If I were to use your alternative, and simply let n be a member
variable which implements ++, then I would not be able to use
polymorphism.

So as far as I can see, D has got it exactly right here.



More information about the Digitalmars-d mailing list