hiding a class property behind a method

luka8088 luka8088 at owave.net
Sun Feb 23 04:51:56 PST 2014


On 22.2.2014. 23:43, simendsjo wrote:
> On 02/22/2014 11:33 PM, Francesco Cattoglio wrote:
> The problem isn't about optional parenthesis or properties. It's the
> fact that
> you can redefine a symbol to be something entierly different, and that this
> difference will only be seen if you are looking at the symbol through the
> "correct" type.

That is exactly what I wanted to point out! I reduced the example code
to make that point more obvious:


import std.stdio;

class A {
  string x = "A";
}

class B : A {
  // should it be legal to hide a symbol and redefine it to a new type?
  int x = 2;
}

//static assert (is(typeof(A.x) == typeof(B.x)));
//Error: static assert (is(string == int)) is false

void main () {
  B o = new B();
  writeln((cast (A)o).x); // A
  writeln((cast (B)o).x); // 2
}


http://dpaste.dzfl.pl/6ae4ac5de1bc



More information about the Digitalmars-d-learn mailing list