www.digitalmars.com         C & C++   DMDScript  

D - 'new' syntax

reply Karl Bochert <kbochert ix.netcom.com> writes:
"D objects are created with the new syntax:
  A a = new A(3);"

This replicates one of my pet peeves. It looks quite standard
and reasonable but I have never created a class named 'A'. More
likely is 'UTIL_New_hash_table_from_list' or some such. The example
is more like:
UTIL_New_hash_table_from_list master_table = new UTIL_New_hash_table_from_list
(startval);


Why not eliminate the redundant class name?
A a = new(3);
i.e.
 UTIL_New_hash_table_from_list master_table = new (startval);

The same applies to assignments

a = new (3)    // 'new' determined by a's type

Karl Bochert
Jan 01 2002
next sibling parent "Pavel Minayev" <evilone omen.ru> writes:
"Karl Bochert" <kbochert ix.netcom.com> wrote in message
news:1103_1009927822 bose...

 Why not eliminate the redundant class name?
 A a = new(3);
 i.e.
  UTIL_New_hash_table_from_list master_table = new (startval);

 The same applies to assignments

 a = new (3)    // 'new' determined by a's type
Sounds like an interesting and useful idea! I would really like to see this implemented in D.
Jan 01 2002
prev sibling next sibling parent Russell Borogove <kaleja estarcion.com> writes:
Karl Bochert wrote:

 "D objects are created with the new syntax:
   A a = new A(3);"
 

 Why not eliminate the redundant class name?
 A a = new(3);
 i.e.
  UTIL_New_hash_table_from_list master_table = new (startval);
 
 The same applies to assignments
 
 a = new (3)    // 'new' determined by a's type
The class name on the new side of the assignment still wants to be optional, at least, for the polymorphic case, as in: Vehicle a = new Car; vehicle_list.add( a ); Vehicle b = new Boat; vehicle_list.add( b ); -RB
Jan 01 2002
prev sibling next sibling parent "Sean L. Palmer" <spalmer iname.com> writes:
Why not make it a property of class references:

class A
{
  this(int x) {}
}

A a;
a.new(3);  // creates a new A and makes a refer to it.

Sean

"Karl Bochert" <kbochert ix.netcom.com> wrote in message
news:1103_1009927822 bose...
 "D objects are created with the new syntax:
   A a = new A(3);"

 This replicates one of my pet peeves. It looks quite standard
 and reasonable but I have never created a class named 'A'. More
 likely is 'UTIL_New_hash_table_from_list' or some such. The example
 is more like:
 UTIL_New_hash_table_from_list master_table = new
UTIL_New_hash_table_from_list (startval);
 Why not eliminate the redundant class name?
 A a = new(3);
 i.e.
  UTIL_New_hash_table_from_list master_table = new (startval);

 The same applies to assignments

 a = new (3)    // 'new' determined by a's type

 Karl Bochert
Jan 02 2002
prev sibling next sibling parent reply Russ Lewis <spamhole-2001-07-16 deming-os.org> writes:
Good idea, but we need to cover some odd cases:

interface I;
class Parent : public I;
class Child : public Parent;
...
I foo = new Child(3);
Parent bar = new Child(3);
Child baz = new Child(3);

In other words, what happens when the result of new is stored in a base class
type?

--
The Villagers are Online! villagersonline.com

.[ (the fox.(quick,brown)) jumped.over(the dog.lazy) ]
.[ (a version.of(English).(precise.more)) is(possible) ]
?[ you want.to(help(develop(it))) ]
Jan 03 2002
parent "Pavel Minayev" <evilone omen.ru> writes:
"Russ Lewis" <spamhole-2001-07-16 deming-os.org> wrote in message
news:3C34BAF6.776B3B55 deming-os.org...

 In other words, what happens when the result of new is stored in a base
class type? In this case, you have to define the classname explicitly (the normal form of new operator). However, since most frequently you want to create a new object and store the result in the variable of the same type, you can use the short form.
Jan 03 2002
prev sibling parent reply "Carlos Santander B." <carlos8294 msn.com> writes:
"Karl Bochert" <kbochert ix.netcom.com> escribió en el mensaje
news:1103_1009927822 bose...
| "D objects are created with the new syntax:
|   A a = new A(3);"
|
| This replicates one of my pet peeves. It looks quite standard
| and reasonable but I have never created a class named 'A'. More
| likely is 'UTIL_New_hash_table_from_list' or some such. The example
| is more like:
| UTIL_New_hash_table_from_list master_table = new
UTIL_New_hash_table_from_list (startval);
|
|
| Why not eliminate the redundant class name?
| A a = new(3);
| i.e.
|  UTIL_New_hash_table_from_list master_table = new (startval);
|
| The same applies to assignments
|
| a = new (3)    // 'new' determined by a's type
|
| Karl Bochert
|
|

I must say I like it. Walter never said anything on (or is it in? damn
prepositions...) this thread...

-------------------------
Carlos Santander


---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.463 / Virus Database: 262 - Release Date: 2003-03-17
Mar 23 2003
parent reply Ilya Minkov <midiclub 8ung.at> writes:
There's a slight problem with it. I first wondered why you have to write =

the class name both at declaration and at initialisation, but then it=20
became evident to me: given i want to store a list of strings. I want=20
its type to be TStrings which is an abstract collection of strings and=20
can't be instantiated. However, i'm not exactly sure how they should be=20
stored. It can be later initialised with TStringList which is an array=20
of strings, or possibly TSringsFromResource or even TStringsFromHardware =

or TDOSVariables and such nonsense. :) Whatever inherits it.

This could however be done in some cases... Especially in the case of=20
object-like structs, and sometimes in the case of objects. If it doesn't =

break the parser too much. But i soehow think that if it would ever be=20
done, it's the last feature to do.

-i.

Carlos Santander B. wrote:
 "Karl Bochert" <kbochert ix.netcom.com> escribi=F3 en el mensaje
 news:1103_1009927822 bose...
 | "D objects are created with the new syntax:
 |   A a =3D new A(3);"
 |
 | This replicates one of my pet peeves. It looks quite standard
 | and reasonable but I have never created a class named 'A'. More
 | likely is 'UTIL_New_hash_table_from_list' or some such. The example
 | is more like:
 | UTIL_New_hash_table_from_list master_table =3D new
 UTIL_New_hash_table_from_list (startval);
 |
 |
 | Why not eliminate the redundant class name?
 | A a =3D new(3);
 | i.e.
 |  UTIL_New_hash_table_from_list master_table =3D new (startval);
 |
 | The same applies to assignments
 |
 | a =3D new (3)    // 'new' determined by a's type
 |
 | Karl Bochert
 |
 |
=20
 I must say I like it. Walter never said anything on (or is it in? damn
 prepositions...) this thread...
Your prepositions are OK. You just have to become clear about what you=20 want to say. :>
Mar 23 2003
next sibling parent "Carlos Santander B." <carlos8294 msn.com> writes:
"Ilya Minkov" <midiclub 8ung.at> escribió en el mensaje
news:b5kpd2$1cs0$1 digitaldaemon.com...
| There's a slight problem with it. I first wondered why you have to write
| the class name both at declaration and at initialisation, but then it
| became evident to me: given i want to store a list of strings. I want
| its type to be TStrings which is an abstract collection of strings and
| can't be instantiated. However, i'm not exactly sure how they should be
| stored. It can be later initialised with TStringList which is an array
| of strings, or possibly TSringsFromResource or even TStringsFromHardware
| or TDOSVariables and such nonsense. :) Whatever inherits it.
|
| This could however be done in some cases... Especially in the case of
| object-like structs, and sometimes in the case of objects. If it doesn't
| break the parser too much. But i soehow think that if it would ever be
| done, it's the last feature to do.
|
| -i.

Already answered:

"Pavel Minayev" <evilone omen.ru> escribió en el mensaje
news:a12e9o$r2j$1 digitaldaemon.com...
| "Russ Lewis" <spamhole-2001-07-16 deming-os.org> wrote in message
| news:3C34BAF6.776B3B55 deming-os.org...
|
| > In other words, what happens when the result of new is stored in a base
| class type?
|
| In this case, you have to define the classname explicitly (the
| normal form of new operator). However, since most frequently
| you want to create a new object and store the result in the
| variable of the same type, you can use the short form.
|
|

-------------------------
Carlos Santander


---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.463 / Virus Database: 262 - Release Date: 2003-03-17
Mar 23 2003
prev sibling parent reply factory <tehdasX optushome.com.au> writes:
In article <b5kpd2$1cs0$1 digitaldaemon.com>, midiclub 8ung.at says...
 There's a slight problem with it. I first wondered why you have to write
 the class name both at declaration and at initialisation, but then it 
 became evident to me: given i want to store a list of strings. I want 
 its type to be TStrings which is an abstract collection of strings and 
 can't be instantiated. However, i'm not exactly sure how they should be 
 stored. It can be later initialised with TStringList which is an array 
 of strings, or possibly TSringsFromResource or even TStringsFromHardware
 or TDOSVariables and such nonsense. :) Whatever inherits it.
 
 This could however be done in some cases... Especially in the case of 
 object-like structs, and sometimes in the case of objects. If it doesn't
 break the parser too much. But i soehow think that if it would ever be 
 done, it's the last feature to do.
 
I also find it personally annoying that I have to keep on restating my intent in most class declarations. And one does not have to demand that a shorter form of the syntax has to be used.. A x; // x is uninitialized A x1(); // x1 is one instance of A A x2= new B; // x2 is one instance of B I'm also of the opinion that the current difference between structs and classes should be relegated to a declaration attribute, but hey, I'm wacky.. -- - Factory, there is no X in my email.
Mar 24 2003
parent "Walter" <walter digitalmars.com> writes:
"factory" <tehdasX optushome.com.au> wrote in message
news:MPG.18eaac936b6e48b98968a news.digitalmars.com...
   And one does not have to demand that a shorter form of the syntax has
 to be used..

   A x; // x is uninitialized
   A x1(); // x1 is one instance of A
   A x2= new B; // x2 is one instance of B
The problem with the A x1(); syntax is it is syntactically ambiguous with a function declaration.
Mar 30 2003