www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Suggestion: shortcut for 'new X' #2

reply Kristian <kjkilpi gmail.com> writes:
Ok, I think my previous suggestion for an optional shortcut was a bit to=
o  =

strange... For example:

Foo f =3D new Foo;
Bar b =3D new Bar(10);

<->

Foo f =3D new;
Bar b =3D new(10);


Now there has been a proposal of the following (I use 'local' keyword  =

here):

Foo f1 =3D local Foo;  //stack/RAII
Foo f2 =3D new Foo;    //heap

I like the syntax also.


How about if you could *alternately* write:

local Foo f1;
new Foo f2;

This would take care of the redundant class name in the declarations.

(I just hate redundance! ;) Writing class names twice is frustrating;  =

there is a pattern of "X ... X".)

Constructor parameters are put after variable names:

local Bar b(10);
new Bar b(10, true);


If this (or something similar) is not possible, a lot of people (includi=
ng  =

me) could end writing auto typed declarations. For example (auto here  =

means auto type, not RAII):

auto f =3D new Foo;
auto b =3D new Bar(10);

That could be shortened to:

f =3D new Foo;
b =3D new Bar(10);

Which leads to my suggestion:

new Foo f;
new Bar b(10);
Sep 05 2006
parent reply Ivan Senji <ivan.senji_REMOVE_ _THIS__gmail.com> writes:
Kristian wrote:
 
 Ok, I think my previous suggestion for an optional shortcut was a bit 
 too strange... For example:
 
 Foo f = new Foo;
 Bar b = new Bar(10);
 
 <->
 
 Foo f = new;
 Bar b = new(10);
 
 
 Now there has been a proposal of the following (I use 'local' keyword 
 here):
 
 Foo f1 = local Foo;  //stack/RAII
 Foo f2 = new Foo;    //heap
 
 I like the syntax also.
 
 
 How about if you could *alternately* write:
 
 local Foo f1;
 new Foo f2;
 
 This would take care of the redundant class name in the declarations.
 
 (I just hate redundance! ;) Writing class names twice is frustrating; 
 there is a pattern of "X ... X".)
 
 Constructor parameters are put after variable names:
 
 local Bar b(10);
 new Bar b(10, true);
 
 
 If this (or something similar) is not possible, a lot of people 
 (including me) could end writing auto typed declarations. For example 
 (auto here means auto type, not RAII):
 
 auto f = new Foo;
 auto b = new Bar(10);

And I still don't see what is so terribly awful about that?
 
 That could be shortened to:
 
 f = new Foo;
 b = new Bar(10);
 
 Which leads to my suggestion:
 
 new Foo f;
 new Bar b(10);

Hmm, that looks a little to strange to me. It looks more like an expression than a declaration.
Sep 05 2006
next sibling parent Kristian <kjkilpi gmail.com> writes:
On Tue, 05 Sep 2006 21:35:13 +0300, Ivan Senji  =

<ivan.senji_REMOVE_ _THIS__gmail.com> wrote:

 Kristian wrote:
  Ok, I think my previous suggestion for an optional shortcut was a bi=


 too strange... For example:
  Foo f =3D new Foo;
 Bar b =3D new Bar(10);
  <->
  Foo f =3D new;
 Bar b =3D new(10);
   Now there has been a proposal of the following (I use 'local' keywo=


 here):
  Foo f1 =3D local Foo;  //stack/RAII
 Foo f2 =3D new Foo;    //heap
  I like the syntax also.
   How about if you could *alternately* write:
  local Foo f1;
 new Foo f2;
  This would take care of the redundant class name in the declarations=


  (I just hate redundance! ;) Writing class names twice is frustrating=


 there is a pattern of "X ... X".)
  Constructor parameters are put after variable names:
  local Bar b(10);
 new Bar b(10, true);
   If this (or something similar) is not possible, a lot of people  =


 (including me) could end writing auto typed declarations. For example=


 (auto here means auto type, not RAII):
  auto f =3D new Foo;
 auto b =3D new Bar(10);

And I still don't see what is so terribly awful about that?

Hehheh, maybe I'll get used to it over the time if I have to...
  That could be shortened to:
  f =3D new Foo;
 b =3D new Bar(10);
  Which leads to my suggestion:
  new Foo f;
 new Bar b(10);

Hmm, that looks a little to strange to me. It looks more like an =

 expression than a declaration.

local Foo f; looks more like a declaration? Maybe 'new' shouldn't be used with this syntax. (Or use "new var Foo = f;"... or not.) I am trying to found out a syntax that has one type name and a variable = = name, and in that order. int i; local File f; vs. int i; auto f =3D local File;
Sep 05 2006
prev sibling parent reply Derek Parnell <derek psyc.ward> writes:
On Tue, 05 Sep 2006 20:35:13 +0200, Ivan Senji wrote:

 Kristian wrote:
 
 Ok, I think my previous suggestion for an optional shortcut was a bit 
 too strange... For example:
 
 Foo f = new Foo;
 Bar b = new Bar(10);
 
 <->
 
 Foo f = new;
 Bar b = new(10);
 
 Now there has been a proposal of the following (I use 'local' keyword 
 here):
 
 Foo f1 = local Foo;  //stack/RAII
 Foo f2 = new Foo;    //heap
 
 I like the syntax also.
 
 How about if you could *alternately* write:
 
 local Foo f1;
 new Foo f2;
 
 This would take care of the redundant class name in the declarations.
 
 (I just hate redundance! ;) Writing class names twice is frustrating; 
 there is a pattern of "X ... X".)
 
 Constructor parameters are put after variable names:
 
 local Bar b(10);
 new Bar b(10, true);
 
 If this (or something similar) is not possible, a lot of people 
 (including me) could end writing auto typed declarations. For example 
 (auto here means auto type, not RAII):
 
 auto f = new Foo;
 auto b = new Bar(10);

And I still don't see what is so terribly awful about that?
 
 That could be shortened to:
 
 f = new Foo;
 b = new Bar(10);
 
 Which leads to my suggestion:
 
 new Foo f;
 new Bar b(10);

Hmm, that looks a little to strange to me. It looks more like an expression than a declaration.

The suggestion looks good to me. I see the same form ... static int a; new Foo b; so to generalize ... DATADECLARATION :: [STORAGECLASS] TYPE IDENTIFIER [ARGUMENTS] int a; new Foo b; local Bar c; static float e; -- Derek Parnell Melbourne, Australia "Down with mediocrity!"
Sep 05 2006
parent reply Ivan Senji <ivan.senji_REMOVE_ _THIS__gmail.com> writes:
Derek Parnell wrote:
 On Tue, 05 Sep 2006 20:35:13 +0200, Ivan Senji wrote:
 
 Kristian wrote:
 Ok, I think my previous suggestion for an optional shortcut was a bit 
 too strange... For example:

 Foo f = new Foo;
 Bar b = new Bar(10);

 <->

 Foo f = new;
 Bar b = new(10);

 Now there has been a proposal of the following (I use 'local' keyword 
 here):

 Foo f1 = local Foo;  //stack/RAII
 Foo f2 = new Foo;    //heap

 I like the syntax also.

 How about if you could *alternately* write:

 local Foo f1;
 new Foo f2;

 This would take care of the redundant class name in the declarations.

 (I just hate redundance! ;) Writing class names twice is frustrating; 
 there is a pattern of "X ... X".)

 Constructor parameters are put after variable names:

 local Bar b(10);
 new Bar b(10, true);

 If this (or something similar) is not possible, a lot of people 
 (including me) could end writing auto typed declarations. For example 
 (auto here means auto type, not RAII):

 auto f = new Foo;
 auto b = new Bar(10);

 That could be shortened to:

 f = new Foo;
 b = new Bar(10);

 Which leads to my suggestion:

 new Foo f;
 new Bar b(10);

expression than a declaration.

The suggestion looks good to me. I see the same form ... static int a; new Foo b; so to generalize ... DATADECLARATION :: [STORAGECLASS] TYPE IDENTIFIER [ARGUMENTS] int a; new Foo b; local Bar c; static float e;

OK, now I know why it looked strange.. :) Class references when normally created would be null, and you have to assign something to them for them to be something other than null. And with this syntax there is explicit assignment :) But is a bigger problem maybe that there are going to be too many ways to write the same thing? Foo b = new Foo; auto b = new Foo; new Foo b;
Sep 05 2006
parent reply Derek Parnell <derek nomail.afraid.org> writes:
On Wed, 06 Sep 2006 02:20:01 +0200, Ivan Senji wrote:

 Derek Parnell wrote:
 On Tue, 05 Sep 2006 20:35:13 +0200, Ivan Senji wrote:
 
 Kristian wrote:
 Ok, I think my previous suggestion for an optional shortcut was a bit 
 too strange... For example:

 Foo f = new Foo;
 Bar b = new Bar(10);

 <->

 Foo f = new;
 Bar b = new(10);

 Now there has been a proposal of the following (I use 'local' keyword 
 here):

 Foo f1 = local Foo;  //stack/RAII
 Foo f2 = new Foo;    //heap

 I like the syntax also.

 How about if you could *alternately* write:

 local Foo f1;
 new Foo f2;

 This would take care of the redundant class name in the declarations.

 (I just hate redundance! ;) Writing class names twice is frustrating; 
 there is a pattern of "X ... X".)

 Constructor parameters are put after variable names:

 local Bar b(10);
 new Bar b(10, true);

 If this (or something similar) is not possible, a lot of people 
 (including me) could end writing auto typed declarations. For example 
 (auto here means auto type, not RAII):

 auto f = new Foo;
 auto b = new Bar(10);

 That could be shortened to:

 f = new Foo;
 b = new Bar(10);

 Which leads to my suggestion:

 new Foo f;
 new Bar b(10);

expression than a declaration.

The suggestion looks good to me. I see the same form ... static int a; new Foo b; so to generalize ... DATADECLARATION :: [STORAGECLASS] TYPE IDENTIFIER [ARGUMENTS] int a; new Foo b; local Bar c; static float e;

OK, now I know why it looked strange.. :) Class references when normally created would be null, and you have to assign something to them for them to be something other than null. And with this syntax there is explicit assignment :)

Good point. But there doesn't have to be an assignment. For example... local Foo a; // Creates a null Foo object reference new Foo b; // Also creates a null Foo object reference local Foo c(); // Creates a Foo object reference and object instance // on the stack with the dtor called when leaving scope. new Foo d(); // Creates a null Foo object reference and object // instance on heap with no guaranteed dtor call. // This is a shorthand for 'Foo d = new Foo();'
 But is a bigger problem maybe that there are going to be too many ways 
 to write the same thing?
 
 Foo b = new Foo;
 auto b = new Foo;
 new Foo b;

Maybe, but we don't have a way to do 'auto auto ...' yet. -- Derek (skype: derek.j.parnell) Melbourne, Australia "Down with mediocrity!" 6/09/2006 12:01:19 PM
Sep 05 2006
parent Kristian <kjkilpi gmail.com> writes:
On Wed, 06 Sep 2006 05:07:33 +0300, Derek Parnell  =

<derek nomail.afraid.org> wrote:

 On Wed, 06 Sep 2006 02:20:01 +0200, Ivan Senji wrote:

 Derek Parnell wrote:
 On Tue, 05 Sep 2006 20:35:13 +0200, Ivan Senji wrote:

 Kristian wrote:
 Ok, I think my previous suggestion for an optional shortcut was a =





 too strange... For example:

 Foo f =3D new Foo;
 Bar b =3D new Bar(10);

 <->

 Foo f =3D new;
 Bar b =3D new(10);

 Now there has been a proposal of the following (I use 'local' keyw=





 here):

 Foo f1 =3D local Foo;  //stack/RAII
 Foo f2 =3D new Foo;    //heap

 I like the syntax also.

 How about if you could *alternately* write:

 local Foo f1;
 new Foo f2;

 This would take care of the redundant class name in the declaratio=





 (I just hate redundance! ;) Writing class names twice is frustrati=





 there is a pattern of "X ... X".)

 Constructor parameters are put after variable names:

 local Bar b(10);
 new Bar b(10, true);

 If this (or something similar) is not possible, a lot of people
 (including me) could end writing auto typed declarations. For exam=





 (auto here means auto type, not RAII):

 auto f =3D new Foo;
 auto b =3D new Bar(10);

 That could be shortened to:

 f =3D new Foo;
 b =3D new Bar(10);

 Which leads to my suggestion:

 new Foo f;
 new Bar b(10);

expression than a declaration.

The suggestion looks good to me. I see the same form ... static int a; new Foo b; so to generalize ... DATADECLARATION :: [STORAGECLASS] TYPE IDENTIFIER [ARGUMENTS] int a; new Foo b; local Bar c; static float e;

OK, now I know why it looked strange.. :) Class references when normally created would be null, and you have to=


 assign something to them for them to be something other than null.

 And with this syntax there is explicit assignment :)

Good point. But there doesn't have to be an assignment. For example...=

   local Foo a;   // Creates a null Foo object reference
   new   Foo b;   // Also creates a null Foo object reference

   local Foo c();   // Creates a Foo object reference and object instan=

                    // on the stack with the dtor called when leaving  =

 scope.
   new   Foo d();   // Creates a null Foo object reference and object
                    // instance on heap with no guaranteed dtor call.
                    // This is a shorthand for 'Foo d =3D new Foo();'

If you like that it would be possible to create null object references = with this syntax, I think I can live with the extra parenthesis when = creating object instances... ;) The compiler could then do the following checks: local Foo a; new Foo b; a =3D local Foo; //ok a =3D new Foo; //error b =3D local Foo; //error b =3D new Foo; //ok
 But is a bigger problem maybe that there are going to be too many way=


 to write the same thing?

 Foo b =3D new Foo;
 auto b =3D new Foo;
 new Foo b;

Maybe, but we don't have a way to do 'auto auto ...' yet.

I don't think this is a big problem because variable declaration is a = simple thing, easy to read and understand. It's a matter of being = consistent. And if 'local' and 'new' here are treaded as storage classes (i.e. "loca= l = Foo a;" creates a null object reference) and the parenthesis syntax is a= = shortcut notation (i.e. "local Foo a();" creates an object reference and= = an object instance), then we have: Foo a; //a =3D null local Foo b; //b =3D null local Bar c(10); //c =3D local Bar(10) Hmm, what if you could put all the init values inside parenthesis...? Fo= r = example: int a(5); //=3D=3D "int a =3D 5;" Bar b(10); //=3D=3D "Bar b =3D new Bar(10);" Well, I am not sure if I like it. It makes "a(5)" to look like a class = object with a constructor... but one could think that an initialization = is = a some kind of constructor... I have to think about it. Comments?
Sep 06 2006