|
Archives
D Programming
DD.gnu digitalmars.D digitalmars.D.bugs digitalmars.D.dtl digitalmars.D.dwt digitalmars.D.announce digitalmars.D.learn digitalmars.D.debugger C/C++ Programming
c++c++.announce c++.atl c++.beta c++.chat c++.command-line c++.dos c++.dos.16-bits c++.dos.32-bits c++.idde c++.mfc c++.rtl c++.stl c++.stl.hp c++.stl.port c++.stl.sgi c++.stlsoft c++.windows c++.windows.16-bits c++.windows.32-bits c++.wxwindows digitalmars.empire digitalmars.DMDScript |
digitalmars.D - Clumsy class instantiation quibble
Am I right in thinking that it isn't legal to do the following in D to create an object and bind it to a reference variable: TestClass tc(/* args */); instead we're forced to use: TestClass tc = new TestClass(/* args */); Why has the first way been disallowed? I know Java also uses the latter's syntax but it's clumsy, necessitating the repetition of the class name. The only slight anomaly the above has would be having to use TestClass tc(); to mean a reference to an automatically instantiated class object and TestClass tc; to mean an unbound reference, which would be null assigned. Of course the existing way should be kept, but I'd much prefer the shortened form to also be available, as coders will often want to both create a class reference and instantiate an object of this same class. What say the D community? May 18 2004
We've said often that this should be the case for auto classes, with the
addition
(to your example) of the auto keyword, as in
auto TestClass tc(/* args */);
"Harvey" <hstroud ntlworld.com> wrote in message
news:c8d5vo$uog$1 digitaldaemon.com...
May 18 2004
Harvey wrote:Am I right in thinking that it isn't legal to do the following in D to create an object and bind it to a reference variable: TestClass tc(/* args */); instead we're forced to use: TestClass tc = new TestClass(/* args */); Why has the first way been disallowed? I know Java also uses the latter's syntax but it's clumsy, necessitating the repetition of the class name. The only slight anomaly the above has would be having to use TestClass tc(); to mean a reference to an automatically instantiated class object and TestClass tc; to mean an unbound reference, which would be null assigned. Of course the existing way should be kept, but I'd much prefer the shortened form to also be available, as coders will often want to both create a class reference and instantiate an object of this same class. What say the D community? May 18 2004
It's more an aesthetic consideration but it seems a lot of extra typing for such a frequently done thing, and the use of the 'new' operator seems superfluous. For example: SomeClass sc; ... sc = new SomeClass(); Why the 'new' keyword, or would its absense cause an ambiguity? Don't get me wrong, what (little) I've seen and used of the language to date looks really promising, but I guess there's always room for improvements and wart removal, and you can't please all the people all of the time ;-)What say the D community? May 18 2004
On Tue, 18 May 2004 21:18:30 +0100, Harvey wrote:It's more an aesthetic consideration but it seems a lot of extra typing for such a frequently done thing, and the use of the 'new' operator seems superfluous. For example: SomeClass sc; ... sc = new SomeClass(); Why the 'new' keyword, or would its absense cause an ambiguity? Don't get me wrong, what (little) I've seen and used of the language to date looks really promising, but I guess there's always room for improvements and wart removal, and you can't please all the people all of the time ;-)What say the D community? May 18 2004
<snip>SomeClass sc; ... if (conditionX) sc = new SomeClass(); else sc = new SomeClass(Y); May 18 2004
That'd be fine, what I've said wouldn't preclude this. I wasn't proposing
getting rid of reference assignments on separate lines to their declaration,
so you're code would simply read:
SomeClass sc; // Null assigned 'unbound' ref
...
if (conditionX)
sc = SomeClass(); // Calls SomeClass.this()
else
sc = SomeClass(Y); // Calls SomeClass.this(Y)
Likewise the other code fragment becomes:
Base b ;
if (conditionX)
b = derivedX(...) ;
else
b = derivedY(...) ;
"Well it could be that sometimes we don't actually want the object to be
instantiated at the point of its declaration."
As can be seen below, this would remain optional:
SomeClass sc; // Null assigned 'unbound' ref
SomeClass sc(); // Ref var assigned to heap allocated class object
I think that would cover all bases? It's not a ground breaking proposal I'm
suggesting, just more a streamlining of syntax.
Cheers.
(btw: if anyone's interested then another person has started a thread 'three
improvements for constructor syntax' in which he puts the same argument a
little more eloquently then I do :-)
"Juan C" <Juan_member pathlink.com> wrote in message
news:c8ebsf$2upt$1 digitaldaemon.com...
May 19 2004
------------------------------
class Foo
{
public:
static Foo opCall()
{
return new Foo();
}
this()
{
// ...
}
}
Foo f = Foo();
------------------------------
There you go.
-C. Sauls
-Invironz
May 19 2004
Hmm, that's hardly convenient though; so for every class where I wanted this facility I'd have to provide a static opCall() ? There has to be a better way! "C. Sauls" <ibisbasenji yahoo.com> wrote in message news:c8g78m$2m9e$1 digitaldaemon.com... May 19 2004
There is a better way... use the provided and language-consistant 'new' operator. The same which is used to explicitly allocate structures, arrays, etc. Maybe I just don't understand the issue... I could maybe understand a desire to get rid of new for auto-objects, but then again I've even had a case where I re-used an auto variable. (Probably a no-no but it works for now.) Then again, using the static opCall trick is convention-consistant, because its the same way we have to approximate constructors for structures. -C. Sauls -Invironz Harvey wrote:Hmm, that's hardly convenient though; so for every class where I wanted this facility I'd have to provide a static opCall() ? There has to be a better way! May 20 2004
Seems like we've just turned full circle, as part of my suggested improvement to class instantiation involves the removal of the new operator. Using 'new' does make it explicit that an object is (somehow, somewhere) being allocated, but does this need spelling out? By definition, when a constructor gets called this will be the case anyhow, so why add clutter to our programs? In C++ there's an important distinction to be made between stack allocated objects and those allocated on the heap, or to be more accurate, the method of accessing an object; as this is all made transparent in D (unless raw pointers are employed) then this becomes far less relevant. But to repeat myself, my main problem is with the tautological syntax 'MyClass c = new MyClass();'. It's nice that features such as 'super' and 'this' (when used for defining constructors, as opposed to naming them the same as the class a la C++) avoid the tedious, less maintainable class name repetition, so why spoil it by this way of instantiation? I'm not sure if I could use a nonstatic opCall to *almost* do what I'm after: MyClass c; c(1,2,3); Probably not as the 'this' getting passed into the opCall would be invalid, but even so, it would fall short anyhow. It's obvious the D community's divided over this one, so perhaps I'll leave it there! Cheers. "C. Sauls" <ibisbasenji yahoo.com> wrote in message news:c8hoap$2cvv$1 digitaldaemon.com...There is a better way... use the provided and language-consistant 'new' operator. The same which is used to explicitly allocate structures, arrays, etc. Maybe I just don't understand the issue... I could maybe understand a desire to get rid of new for auto-objects, but then again I've even had a case where I re-used an auto variable. (Probably a no-no but it works for now.) Then again, using the static opCall trick is convention-consistant, because its the same way we have to approximate constructors for May 22 2004
But to repeat myself, my main problem is with the tautological syntax 'MyClass c = new MyClass();'. It's nice that features such as 'super' and 'this' (when used for defining constructors, as opposed to naming them the same as the class a la C++) avoid the tedious, less maintainable class name repetition, so why spoil it by this way of instantiation? May 24 2004
Derek Parnell wrote:On Tue, 18 May 2004 21:18:30 +0100, Harvey wrote: May 19 2004
|