digitalmars.D - Why retain new ?
- Alex Burton (7/7) Aug 05 2007 Given that all classes are on the heap, why not replace the syntax :
- Kirk McDonald (11/19) Aug 05 2007 You can already say this:
- Mike Parker (18/36) Aug 05 2007 Another thing is that these are two different function calls in D. With
- Chris Nicholson-Sauls (5/45) Aug 05 2007 And along the lines of the C++ comparison, it might be confusing to some...
- janderson (6/13) Aug 06 2007 I agree its a PITA.
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
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
Kirk McDonald wrote:Alex Burton wrote: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(); } ========================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.
Aug 05 2007
Mike Parker wrote:Kirk McDonald wrote: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-SaulsAlex Burton wrote: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(); } ========================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.
Aug 05 2007
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