www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Why retain new ?

reply Alex Burton <alexibu mac.com> writes:
Given that all classes are on the heap, why not replace the syntax :
CmdLin cl = new CmdLin(argc, argv);
with
CmdLin cl(argc,argv);
saving some typing and repetition.

Only when casting to base class would you want to do :
CmdLinBase cl = CmdLin(argc,argv);
Aug 05 2007
next sibling parent reply Kirk McDonald <kirklin.mcdonald gmail.com> writes:
Alex Burton wrote:
 Given that all classes are on the heap, why not replace the syntax :
 CmdLin cl = new CmdLin(argc, argv);
 with
 CmdLin cl(argc,argv);
 saving some typing and repetition.
 
 Only when casting to base class would you want to do :
 CmdLinBase cl = CmdLin(argc,argv);

You can already say this: auto cl = new CmdLin(argc, argv); The syntax you suggest looks too much like the C++ syntax for allocating on the stack. Using 'new' is more explicit, and makes it abundantly clear where the class is being allocated, and what its lifetime is. -- Kirk McDonald http://kirkmcdonald.blogspot.com Pyd: Connecting D and Python http://pyd.dsource.org
Aug 05 2007
parent reply Mike Parker <aldacron71 yahoo.com> writes:
Kirk McDonald wrote:
 Alex Burton wrote:
 Given that all classes are on the heap, why not replace the syntax :
 CmdLin cl = new CmdLin(argc, argv);
 with
 CmdLin cl(argc,argv);
 saving some typing and repetition.

 Only when casting to base class would you want to do :
 CmdLinBase cl = CmdLin(argc,argv);

You can already say this: auto cl = new CmdLin(argc, argv); The syntax you suggest looks too much like the C++ syntax for allocating on the stack. Using 'new' is more explicit, and makes it abundantly clear where the class is being allocated, and what its lifetime is.

Another thing is that these are two different function calls in D. With the new keyword, you are calling the constructor. Without it, you are calling opCall. So you can simulate this feature by making use of static opCall like so: ======================== class A { static A opCall() { return new A; } } void main() { A a = A(); } ========================
Aug 05 2007
parent Chris Nicholson-Sauls <ibisbasenji gmail.com> writes:
Mike Parker wrote:
 Kirk McDonald wrote:
 Alex Burton wrote:
 Given that all classes are on the heap, why not replace the syntax :
 CmdLin cl = new CmdLin(argc, argv);
 with
 CmdLin cl(argc,argv);
 saving some typing and repetition.

 Only when casting to base class would you want to do :
 CmdLinBase cl = CmdLin(argc,argv);

You can already say this: auto cl = new CmdLin(argc, argv); The syntax you suggest looks too much like the C++ syntax for allocating on the stack. Using 'new' is more explicit, and makes it abundantly clear where the class is being allocated, and what its lifetime is.

Another thing is that these are two different function calls in D. With the new keyword, you are calling the constructor. Without it, you are calling opCall. So you can simulate this feature by making use of static opCall like so: ======================== class A { static A opCall() { return new A; } } void main() { A a = A(); } ========================

And along the lines of the C++ comparison, it might be confusing to some that 'Class var(123);' creates an object while 'Class var;' does not. I like that 'new' sticking out, anyhow. Its good visual aid. -- Chris Nicholson-Sauls
Aug 05 2007
prev sibling parent janderson <askme me.com> writes:
Alex Burton wrote:
 Given that all classes are on the heap, why not replace the syntax :
 CmdLin cl = new CmdLin(argc, argv);
 with
 CmdLin cl(argc,argv);
 saving some typing and repetition.

 Only when casting to base class would you want to do :
 CmdLinBase cl = CmdLin(argc,argv);

I agree its a PITA. I wounder maybe you could write a template: CmdLin cl = New(argc, argv); of course you can do: auto cl = new CmdLin(argc, argv);
Aug 06 2007