www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - typedef-ed ctor [feature enhancement]

reply typedef d.com writes:
Suppose a class has ctor defined, then a typedef-ed class cannot use that ctor:


------------------------------------------------------------  
class A { public this(int param) {} }   

typedef A B;   

#void foo() {   
#  A a = new A(0);  // OK   
#  B b = new B(0);  // error: cannot implicitly convert expression   
#                   // (new A (0)) of type typedef.A to B   
#}   
------------------------------------------------------------  

by contrast, if no ctor defined in A, then it works:  

------------------------------------------------------------  
class A {}   

typedef A B;   

void foo() {  
A a = new A();  //OK  
B b = new B();  //OK  
}  
------------------------------------------------------------  

I think we should make the first example also work, otherwise many non-trivial  
typedef-ed class cannot call ctor; this will make typedef less useful (people 
will start to use alias everywhere).  So the desired behaviour should be:  

B b = new A();  // wrong! expected: you cannot assign A to B 
B b = new B();  // should work here, because B is B! 
May 09 2005
parent "Andrew Fedoniouk" <news terrainformatica.com> writes:
 #  B b = new B(0);  // error: cannot implicitly convert expression
 #                   // (new A (0)) of type typedef.A to B

For me it looks like just a bug of current implementation. ------------------------------------------ <typedef d.com> wrote in message news:d5o9mb$17ok$1 digitaldaemon.com...
 Suppose a class has ctor defined, then a typedef-ed class cannot use that 
 ctor:


 ------------------------------------------------------------
 class A { public this(int param) {} }

 typedef A B;

 #void foo() {
 #  A a = new A(0);  // OK
 #  B b = new B(0);  // error: cannot implicitly convert expression
 #                   // (new A (0)) of type typedef.A to B
 #}
 ------------------------------------------------------------

 by contrast, if no ctor defined in A, then it works:

 ------------------------------------------------------------
 class A {}

 typedef A B;

 void foo() {
 A a = new A();  //OK
 B b = new B();  //OK
 }
 ------------------------------------------------------------

 I think we should make the first example also work, otherwise many 
 non-trivial
 typedef-ed class cannot call ctor; this will make typedef less useful 
 (people
 will start to use alias everywhere).  So the desired behaviour should be:

 B b = new A();  // wrong! expected: you cannot assign A to B
 B b = new B();  // should work here, because B is B!



 

May 09 2005