how to make new C return other static type than C

Ali Çehreli acehreli at yahoo.com
Sun Dec 9 20:42:38 PST 2012


On 12/09/2012 12:02 PM, deed wrote:
 > void main()
 > {
 > auto c = new C;

D is a strongly statically typed language. The type of c is C, period. 
The compiler will compile the following lines of code all under that 
observation.

 > c.setx(5); // Should set x to 5 through i1
 > c.getSum(); // Should return 5. (5 + 0)
 > c.sety(3); // Should not be possible
 > c.switchInterface() // currentInterface is now i2

The compiler cannot analyze the implementation of switchInterface(). c 
is still a C.

 > c.setx(10); // Should not be possible
 > c.sety(3); // Should set y to 8 through i2
 > c.getSum(); // Should return 8. (5 + 3)
 > }
 >
 >
 > - Could this be possible?

Not as you have shown.

 > - Will i1 and i2 expose different interfaces to the same object?

Yes but it looks like they are not helping with what you are trying to 
achieve.

 > - How can a method call on an object be directed to an internal
 > object/interface?

Sometimes explicitly:

class C
{
     Member m;

     void foo()
     {
         m.foo();
     }
}

Sometimes automatically by alias this:

class C
{
     Member m;

     alias m this;
}

There must be other more suitable methods depending on the situation.

Ali



More information about the Digitalmars-d-learn mailing list