hiding a class property behind a method

simendsjo simendsjo at gmail.com
Sat Feb 22 13:45:46 PST 2014


On 02/22/2014 09:43 PM, Ali Çehreli wrote:
> It looks like name hiding, which I am familiar from C++. Name hiding
> does not differentiate between functions and variables.
>
> Ali
>

The problem is that hiding a name *is* a problem. When you are hiding a 
name,
then a class would no longer behave as you would expect. It breaks LSP in
a pretty awful way, and suddenly the *type* of a symbol changes based on 
what
superclass you happened to use for a reference.

     class A {
         void f() {}
     }

     class B : A {
         int f;
     }

     A b = new B();
     writeln(typeof(b.f).stringof); // void()

     B veryB = cast(B)b;
     writeln(typeof(veryB.f).stringof); // int

Now suddenly, it's very important to use B as the type for a reference. 
This is
very dangerous behavior in my opinion, and I think I've only used it 
*once* in
C# - which requires you to be explicit about it - and I still feel dirty.

Now what if a superclass implements a symbol that you are using in a 
subclass?
I say we force it to be explicit as we finally did with `override`, which is
shows some of the same issues, although not nearly as dangerous and hidden.
I think member hiding is nearly always a bug, and we should be very explicit
about it.


More information about the Digitalmars-d-learn mailing list