Static opCall and class instantiation

Rémy Mouëza Rémy_member at pathlink.com
Sat May 20 18:15:08 PDT 2006


In article <e4o55p$20g$1 at digitaldaemon.com>, Kirk McDonald says...
>Python does exactly this. Like D (and Java, for that matter), class 
>instances can only be created by reference, meaning the new keyword is 
>redundant, and so the language dropped it entirely. I never understood 
>why Java bothered to keep it, and I still don't get it in D. It's still 
>useful for structs, which can be put on the stack, yes? So I think D 
>should support both syntaxes.

In the Python expression "ojbect = ClassName ()", ClassName is a meta class
object on wich one calls the __call__ method that is the opCall equivalent. This
methode will call the __new__ method that allocates a new object, inits its
primary fields: class name, super classes, dictionnary of methods and
attributes. Then it calls the __init__ method of that object wich is like the
body of a D constructor. 
D type system is not as developped as Python in meta type information ( or
reflexivity ). If one want to change the allocating behaviour of a class, he/she
has to override the 'new' constructor like method in the class. In Python this
would be done in the meta class, with the __new__ method. 
That's why I think that the 'new' keyword still make sense in D.

>Because there is only one way to instantiate a class, all of these 
>different syntaxes mean the same thing. The "new" keyword is not needed, 
>but should still be kept. And if the various Python code I've read is 
>any guide, dropping the "new" keyword is still eminently clear for the 
>reader.

I agree. 

>As far as I could tell, the original thread never really came to a 
>conclusion. I was wondering what the D community thought of it now, 
>almost exactly two years later. Thoughts?

Keeping the 'new' keyword allows to change the allocation behaviour of class,
passing some arguments to new like: 'new (1, 2, 3 ) World ( "hello");'. As few
people ever modify the allocation behaviour, a factory method like Python would
make an easier to read code without an heavy break of the semantic, but the
static opCall could no more be used for something else than allocation and
initialization.

>-Kirk McDonald 





More information about the Digitalmars-d mailing list