www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Forbid the use of method name "init"

reply Frank Benoit <keinfarbton googlemail.com> writes:
"init" and other class properties are kind of shadow-keywords.

class MyClass{
   void init(){
   }
}

The class is fine and can be used.
Now someone attempts to insert this class into a container....

template Templ(T){
   T t = T.init; // Compile error
}

The compiler should forbid the use of the method name "init", if the 
signature does not make any sense for an "init" property. It must be a 
static method, with a return value and no arguments.
May 10 2008
parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"Frank Benoit" <keinfarbton googlemail.com> wrote in message 
news:g052dt$qt4$1 digitalmars.com...
 "init" and other class properties are kind of shadow-keywords.

 class MyClass{
   void init(){
   }
 }

 The class is fine and can be used.
 Now someone attempts to insert this class into a container....

 template Templ(T){
   T t = T.init; // Compile error
 }

 The compiler should forbid the use of the method name "init", if the 
 signature does not make any sense for an "init" property. It must be a 
 static method, with a return value and no arguments.

This should extend to any semi-reserved name. The compiler used to allow redefinition of 'sizeof' and 'offsetof' but checks were added to disallow that. That mechanism should just be extended.
May 10 2008
parent reply davidl 126.com writes:
=D4=DA Sun, 11 May 2008 05:46:57 +0800=A3=ACJarrett Billingsley  =

<kb3ctd2 yahoo.com> =D0=B4=B5=C0:

 "Frank Benoit" <keinfarbton googlemail.com> wrote in message
 news:g052dt$qt4$1 digitalmars.com...
 "init" and other class properties are kind of shadow-keywords.

 class MyClass{
   void init(){
   }
 }

 The class is fine and can be used.
 Now someone attempts to insert this class into a container....

 template Templ(T){
   T t =3D T.init; // Compile error
 }

 The compiler should forbid the use of the method name "init", if the
 signature does not make any sense for an "init" property. It must be =


 static method, with a return value and no arguments.

This should extend to any semi-reserved name. The compiler used to al=

 redefinition of 'sizeof' and 'offsetof' but checks were added to disal=

 that.  That mechanism should just be extended.

I filed a bug of forbiding the use of stringof method. But it's markd as= = invalid :( http://d.puremagic.com/issues/show_bug.cgi?id=3D1412 -- = =CA=B9=D3=C3 Opera =B8=EF=C3=FC=D0=D4=B5=C4=B5=E7=D7=D3=D3=CA=BC=FE=BF=CD= =BB=A7=B3=CC=D0=F2: http://www.opera.com/mail/
May 10 2008
parent Bruno Medeiros <brunodomedeiros+spam com.gmail> writes:
davidl 126.com wrote:
  Sun, 11 May 2008 05:46:57 +0800Jarrett Billingsley 
 <kb3ctd2 yahoo.com> д:
 
 "Frank Benoit" <keinfarbton googlemail.com> wrote in message
 news:g052dt$qt4$1 digitalmars.com...
 "init" and other class properties are kind of shadow-keywords.

 class MyClass{
   void init(){
   }
 }

 The class is fine and can be used.
 Now someone attempts to insert this class into a container....

 template Templ(T){
   T t = T.init; // Compile error
 }

 The compiler should forbid the use of the method name "init", if the
 signature does not make any sense for an "init" property. It must be a
 static method, with a return value and no arguments.

This should extend to any semi-reserved name. The compiler used to allow redefinition of 'sizeof' and 'offsetof' but checks were added to disallow that. That mechanism should just be extended.

I filed a bug of forbiding the use of stringof method. But it's markd as invalid :( http://d.puremagic.com/issues/show_bug.cgi?id=1412

I've added my reasoning to that bug report. I guess one could make a case for redefining 'init', but as for 'stringof', it should be the same as 'sizeof' and 'offsetof': unextendable, like Jarret mentioned. -- Bruno Medeiros - Software Developer, MSc. in CS/E graduate http://www.prowiki.org/wiki4d/wiki.cgi?BrunoMedeiros#D
May 27 2008