www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Abreviated instantiation

reply "TechnoZeus" <TechnoZeus PeoplePC.com> writes:
I know this has been menioned before, but it never seems to get much attention,
or any viable solutions (that I have seen, at least) so here's a suggestion...
( inspired by reading the code sample at...
http://www.terrainformatica.com/wiki/pmwiki.php?pagename=Harmonia.Harmonia )

How about allowing something like...
HtmlWindow w = new HtmlWindow;
to be written instead as...
new HtmlWindow w;
as an alternative syntax?

I don't think it should be difficult for the compiler to figure it out,
since the "new" command usually only has a single item after it.
Correct me if I'm mistaken.

Likewise,
Xclass x = new Xclass; x = whatever;
could be written as...
new Xclass x = whatever;

Any thoughts or comments?

TZ
May 28 2005
parent reply "TechnoZeus" <TechnoZeus PeoplePC.com> writes:
"TechnoZeus" <TechnoZeus PeoplePC.com> wrote in message
news:d79mrf$l41$1 digitaldaemon.com...
 I know this has been menioned before, but it never seems to get much attention,
 or any viable solutions (that I have seen, at least) so here's a suggestion...
 ( inspired by reading the code sample at...
http://www.terrainformatica.com/wiki/pmwiki.php?pagename=Harmonia.Harmonia )

 How about allowing something like...
 HtmlWindow w = new HtmlWindow;
 to be written instead as...
 new HtmlWindow w;
 as an alternative syntax?

 I don't think it should be difficult for the compiler to figure it out,
 since the "new" command usually only has a single item after it.
 Correct me if I'm mistaken.

 Likewise,
 Xclass x = new Xclass; x = whatever;
 could be written as...
 new Xclass x = whatever;

 Any thoughts or comments?

 TZ

Just now (coincidentally) looking pack at some older posts in this newsgroup, I came across a link to http://all-technology.com/eigenpolls/dwishlist/ which in turn has a link to... http://all-technology.com/eigenpolls/dwishlist/index.php?it=18 which lists the following proposed syntax variations for instantiation... MyClassName a; //a is null MyClassName a(); //same as MyClassName a = new MyClassName(); MyClassName a(1,2); //same as MyClassName a = new MyClassName(1,2); MyClassName a = new MyClassName(1,2,3); //obvious :) I like these, but I can see some possible concern over whether or not some of them would be obvious enough that they were creating a new instance of a class. The alternative syntax that I am proposing could be added to the list as... new MyClassName a; // same as MyClassName a = new MyClassName; new MyClassName() a; // same as MyClassName a = new MyClassName(); new MyClassName(1,2) a; // same as MyClassName a = new MyClassName(1,2); new MyClassName a = new MyClassName(1,2,3) a; // same as MyClassName a = new MyClassName(1,2,3); TZ
May 28 2005
parent "TechnoZeus" <TechnoZeus PeoplePC.com> writes:
oops...
The last one, I somehow failed to edit correctly or something.  I'll fix it
here and re-post as a reply.

The alternative syntax that I am proposing could be added to the list as...

new MyClassName a; // same as MyClassName a = new MyClassName;
new MyClassName() a; // same as MyClassName a = new MyClassName();
new MyClassName(1,2) a; // same as MyClassName a = new MyClassName(1,2);
new MyClassName(1,2,3) a; // same as MyClassName a = new MyClassName(1,2,3);

Of course, the existing syntax would still be needed for things like...
MyClassName a = new MyOtherClassName(1,2,3);
MyClassName b = new SomeDifferentClass;

although this could perhaps be accomplished also by something like...
new cast(MyClassName) MyOtherClassName(1,2,3) a;
new cast(MyClassName) SomeDifferentClass b;

... I think.  Hehe.  Hard to say where the "new" should go in a case like that.
cast(MyClassName) new MyOtherClassName(1,2,3) a;
cast(MyClassName) new SomeDifferentClass b;
Perhaps? But I think the old syntax is sufficient for such cases anyway.

It just makes sense to have a shorter form of the common instantiation where
the same class name
otherwise ends up having to be repeated.

TZ
May 28 2005