www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - shorter alternative of constructor with parameter

reply "Suliman" <evermind live.ru> writes:
Dart and few others modern languages support short declaration 
constructor with parameter:

class Person {
   String name;

   Person(String name) {
     this.name = name;
   }
}

// Shorter alternative
class Person {
   String name;

   // parameters prefixed by 'this.' will assign to
   // instance variables automatically
   Person(this.name);
}

it's there any DIP for adding this future to D?
Jun 21 2014
next sibling parent "bearophile" <bearophileHUGS lycos.com> writes:
Suliman:

 // Shorter alternative
 class Person {
   String name;

   // parameters prefixed by 'this.' will assign to
   // instance variables automatically
   Person(this.name);
 }

 it's there any DIP for adding this future to D?
This was already discussed once or more in past. Take a look in Bugzilla, there is an ER. I think with some improvements this idea is a good thing for D. An even shorter syntax: class Person { this(const this.name) {} } Bye, bearophile
Jun 21 2014
prev sibling next sibling parent reply "Xinok" <xinok live.com> writes:
On Saturday, 21 June 2014 at 17:17:57 UTC, Suliman wrote:
 Dart and few others modern languages support short declaration 
 constructor with parameter:

 class Person {
   String name;

   Person(String name) {
     this.name = name;
   }
 }

 // Shorter alternative
 class Person {
   String name;

   // parameters prefixed by 'this.' will assign to
   // instance variables automatically
   Person(this.name);
 }

 it's there any DIP for adding this future to D?
I'd prefer that we didn't crowd the language with minor shortcuts like these, and save syntactic sugar for more useful features. Plus, it would be easy enough to make a string mixin which generates such boilerplate code.
Jun 21 2014
next sibling parent reply "Suliman" <evermind live.ru> writes:
and save syntactic sugar for more useful features.
How not implementing this future will help to save syntactic sugar?
Jun 21 2014
next sibling parent reply "Xinok" <xinok live.com> writes:
On Saturday, 21 June 2014 at 18:57:25 UTC, Suliman wrote:
and save syntactic sugar for more useful features.
How not implementing this future will help to save syntactic sugar?
I mean to use syntactic sugar sparingly, not for minor features like this. The trouble is that it makes code non-verbose, which would make D code more difficult to read for those unfamiliar with the language.
Jun 21 2014
parent reply "Suliman" <evermind live.ru> writes:
 I mean to use syntactic sugar sparingly, not for minor features 
 like this. The trouble is that it makes code non-verbose, which 
 would make D code more difficult to read for those unfamiliar 
 with the language.
Less code make code more easy to read. I do not think that such reduce amount of code make code-understanding harder. I am sure that in Dart 80% (or even more) programmers will use reduced constructor.
Jun 21 2014
next sibling parent "Ola Fosheim =?UTF-8?B?R3LDuHN0YWQi?= writes:
On Sunday, 22 June 2014 at 06:27:52 UTC, Suliman wrote:
 I am sure that in Dart 80% (or even more) programmers will use 
 reduced constructor.
Yep, it encourage tuples with named fields. Which makes code more readable.
Jun 21 2014
prev sibling parent "Xinok" <xinok live.com> writes:
On Sunday, 22 June 2014 at 06:27:52 UTC, Suliman wrote:
 I mean to use syntactic sugar sparingly, not for minor 
 features like this. The trouble is that it makes code 
 non-verbose, which would make D code more difficult to read 
 for those unfamiliar with the language.
Less code make code more easy to read. I do not think that such reduce amount of code make code-understanding harder. I am sure that in Dart 80% (or even more) programmers will use reduced constructor.
Part of my problem with this proposal is that its use case is very narrow and limited to this specific scenario. If you (or somebody) could generalize it so it has more use cases, then I might support it.
Jun 22 2014
prev sibling parent "Dicebot" <public dicebot.lv> writes:
On Saturday, 21 June 2014 at 18:57:25 UTC, Suliman wrote:
and save syntactic sugar for more useful features.
How not implementing this future will help to save syntactic sugar?
There is a limited amount of non-widespread syntax sugar you can fit into language without harming its learning curve. By spending it on features that don't make big difference you make language over-saturated by the time something really important becomes necessary.
Jun 21 2014
prev sibling next sibling parent reply Jonathan M Davis via Digitalmars-d <digitalmars-d puremagic.com> writes:
On Sat, 21 Jun 2014 18:50:21 +0000
Xinok via Digitalmars-d <digitalmars-d puremagic.com> wrote:

 On Saturday, 21 June 2014 at 17:17:57 UTC, Suliman wrote:
 Dart and few others modern languages support short declaration
 constructor with parameter:

 class Person {
   String name;

   Person(String name) {
     this.name = name;
   }
 }

 // Shorter alternative
 class Person {
   String name;

   // parameters prefixed by 'this.' will assign to
   // instance variables automatically
   Person(this.name);
 }

 it's there any DIP for adding this future to D?
I'd prefer that we didn't crowd the language with minor shortcuts like these, and save syntactic sugar for more useful features. Plus, it would be easy enough to make a string mixin which generates such boilerplate code.
Agreed. This would just add more stuff to the language that people would have to understand, and it really doesn't add much benefit. It's just a slightly terser syntax - and one that doesn't fit in with any other kind of function declarations in D to boot. - Jonathan M Davis
Jun 21 2014
next sibling parent "bearophile" <bearophileHUGS lycos.com> writes:
Jonathan M Davis:

 Agreed. This would just add more stuff to the language that 
 people would have to understand,
Now there are languages with such idea, like TypeScript and Scala, so I think programmers can grasp this simple idea quickly.
 and it really doesn't add much benefit. It's just a slightly 
 terser syntax -
It's a more dry syntax, it saves typing, reduces the noise, and makes the code less bug-prone because it's more DRY, you have to repeat similar things less times. So I think it's a good idea for D. It also partially replaces my idea of giving a warning/error when you have arguments equal to field names, because it removes a source for a common bug (this code compiles with no errors nor warnings and shows a different but related bug): struct Foo { int x; this(in int x_) { x = x; } } void main() {} Bye, bearophile
Jun 21 2014
prev sibling parent reply "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Sat, 21 Jun 2014 15:47:03 -0400, Jonathan M Davis via Digitalmars-d  
<digitalmars-d puremagic.com> wrote:

 On Sat, 21 Jun 2014 18:50:21 +0000
 Xinok via Digitalmars-d <digitalmars-d puremagic.com> wrote:

 On Saturday, 21 June 2014 at 17:17:57 UTC, Suliman wrote:
 Dart and few others modern languages support short declaration
 constructor with parameter:

 class Person {
   String name;

   Person(String name) {
     this.name = name;
   }
 }

 // Shorter alternative
 class Person {
   String name;

   // parameters prefixed by 'this.' will assign to
   // instance variables automatically
   Person(this.name);
 }

 it's there any DIP for adding this future to D?
I'd prefer that we didn't crowd the language with minor shortcuts like these, and save syntactic sugar for more useful features. Plus, it would be easy enough to make a string mixin which generates such boilerplate code.
Agreed. This would just add more stuff to the language that people would have to understand, and it really doesn't add much benefit. It's just a slightly terser syntax - and one that doesn't fit in with any other kind of function declarations in D to boot.
Yeah, I don't think we save much with this. A mixin should be able to assign all the names given in the parameters that you name the same way. In fact, I bet one can write a boiler-plate string that works for ANY class, using __traits(allMembers), as long as your parameter names match the member names. Then you just mixin that string as the first thing in the ctor. -Steve
Jun 23 2014
parent "H. S. Teoh via Digitalmars-d" <digitalmars-d puremagic.com> writes:
On Mon, Jun 23, 2014 at 11:30:22AM -0400, Steven Schveighoffer via
Digitalmars-d wrote:
 On Sat, 21 Jun 2014 15:47:03 -0400, Jonathan M Davis via Digitalmars-d
 <digitalmars-d puremagic.com> wrote:
 
On Sat, 21 Jun 2014 18:50:21 +0000
Xinok via Digitalmars-d <digitalmars-d puremagic.com> wrote:

On Saturday, 21 June 2014 at 17:17:57 UTC, Suliman wrote:
 Dart and few others modern languages support short declaration
 constructor with parameter:

 class Person {
   String name;

   Person(String name) {
     this.name = name;
   }
 }

 // Shorter alternative
 class Person {
   String name;

   // parameters prefixed by 'this.' will assign to
   // instance variables automatically
   Person(this.name);
 }

 it's there any DIP for adding this future to D?
I'd prefer that we didn't crowd the language with minor shortcuts like these, and save syntactic sugar for more useful features. Plus, it would be easy enough to make a string mixin which generates such boilerplate code.
Agreed. This would just add more stuff to the language that people would have to understand, and it really doesn't add much benefit. It's just a slightly terser syntax - and one that doesn't fit in with any other kind of function declarations in D to boot.
Yeah, I don't think we save much with this. A mixin should be able to assign all the names given in the parameters that you name the same way. In fact, I bet one can write a boiler-plate string that works for ANY class, using __traits(allMembers), as long as your parameter names match the member names. Then you just mixin that string as the first thing in the ctor.
[...] Here's a first stab at a working mixin that does this (uncomment the pragma(msg,...) lines to see the generated code): string defaultCtor(T)() if (is(T == class)) { enum isDataMember(T, string memb) = is(typeof(typeof(__traits(getMember, T, memb)).init)); // Generate function signature string params, ctorBody; string delim = ""; foreach (memb; __traits(derivedMembers, T)) { static if (isDataMember!(T, memb)) { alias argtype = typeof(__traits(getMember, T, memb)); auto argname = "_" ~ memb; params ~= delim ~ argtype.stringof ~ " " ~ argname; delim = ", "; ctorBody ~= " " ~ memb ~ " = " ~ argname ~ ";\n"; } } return "this(" ~ params ~ ")\n{\n" ~ ctorBody ~ "}"; } class C { string name; int age; bool registered; mixin(defaultCtor!(typeof(this))); //pragma(msg, defaultCtor!(typeof(this))); void myMethod() {} } class D { int x, y; string label; mixin(defaultCtor!(typeof(this))); //pragma(msg, defaultCtor!(typeof(this))); } void main() { auto c = new C("John Doe", 30, true); // oh yeah auto d = new D(10, 20, "Node 1"); // rock on! ;-) } Currently, this mixin doesn't handle inheritance very well, but it should be trivial to extend it to collect all superclass data members and package them off into a super(...) call inside the generated ctor. D rawckz. T -- Making non-nullable pointers is just plugging one hole in a cheese grater. -- Walter Bright
Jun 23 2014
prev sibling parent reply "Gary Willoughby" <dev nomad.so> writes:
On Saturday, 21 June 2014 at 18:50:23 UTC, Xinok wrote:
 I'd prefer that we didn't crowd the language with minor 
 shortcuts like these, and save syntactic sugar for more useful 
 features. Plus, it would be easy enough to make a string mixin 
 which generates such boilerplate code.
I agree please don't add shortcuts like this that really don't add anything other than more complexity. Please keep D simple and clean to read and have to always guess what shortcuts are being used. As another dev said a template or mixin can be used for this.
Jun 23 2014
parent reply "bearophile" <bearophileHUGS lycos.com> writes:
Gary Willoughby:

 As another dev said a template or mixin can be used for this.
I don't think a template/mixin can replace that sufficiently well, because you also often want to put some code inside the constructor. Bye, bearophile
Jun 23 2014
next sibling parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Mon, 23 Jun 2014 14:20:32 -0400, bearophile <bearophileHUGS lycos.com>  
wrote:

 Gary Willoughby:

 As another dev said a template or mixin can be used for this.
I don't think a template/mixin can replace that sufficiently well, because you also often want to put some code inside the constructor.
The mixin does not have to generate a whole function, it can just generate the assigns inside a ctor. I'm envisioning something like this: class C { int a; int b; int c; this(int a, int b, int c) { mixin(ctorAssigns); // this.a = a; this.b = b; this.c = c; // other code } } -Steve
Jun 23 2014
prev sibling parent reply "Ola Fosheim =?UTF-8?B?R3LDuHN0YWQi?= writes:
On Monday, 23 June 2014 at 18:20:33 UTC, bearophile wrote:
 I don't think a template/mixin can replace that sufficiently 
 well, because you also often want to put some code inside the 
 constructor.
And it is unreadable. "this.x" is very useful for creating new classes in Dart when you build up the main structure of your program. I don't think "this.x" should be considered syntactical sugar. That seems semantically like a wrong conclusion. You just specify the aggregate variable directly instead of going by a temporary. Is "a=b" syntactical sugar for "tmp = b; a = b"? No. Ola.
Jun 29 2014
parent "Ola Fosheim =?UTF-8?B?R3LDuHN0YWQi?= writes:
On Sunday, 29 June 2014 at 21:37:29 UTC, Ola Fosheim Grøstad 
wrote:

 Is "a=b" syntactical sugar for "tmp = b; a = b"? No.
I meant "tmp=b; a=tmp", of course… :-P
Jun 29 2014
prev sibling next sibling parent reply "Kapps" <opantm2+spam gmail.com> writes:
On Saturday, 21 June 2014 at 17:17:57 UTC, Suliman wrote:
 Dart and few others modern languages support short declaration 
 constructor with parameter:

 class Person {
   String name;

   Person(String name) {
     this.name = name;
   }
 }

 // Shorter alternative
 class Person {
   String name;

   // parameters prefixed by 'this.' will assign to
   // instance variables automatically
   Person(this.name);
 }

 it's there any DIP for adding this future to D?
Personally I'd definitely welcome this syntax. It's an extremely common thing to do, prone to typos / bugs, is a simple syntax, and is something I'm surprised more languages don't have.
Jun 21 2014
parent reply "SomeDude" <lolilol mytrashmail.com> writes:
On Saturday, 21 June 2014 at 21:25:33 UTC, Kapps wrote:
 Personally I'd definitely welcome this syntax. It's an 
 extremely common thing to do, prone to typos / bugs, is a 
 simple syntax, and is something I'm surprised more languages 
 don't have.
I've never seen a single instance of a bug like this in 15 years of Java and C++ programming. One has to be really sloppy in order to insert bugs in such simple code.
Jun 21 2014
next sibling parent "H. S. Teoh via Digitalmars-d" <digitalmars-d puremagic.com> writes:
On Sat, Jun 21, 2014 at 09:47:00PM +0000, SomeDude via Digitalmars-d wrote:
 On Saturday, 21 June 2014 at 21:25:33 UTC, Kapps wrote:
Personally I'd definitely welcome this syntax. It's an extremely
common thing to do, prone to typos / bugs, is a simple syntax, and is
something I'm surprised more languages don't have.
I've never seen a single instance of a bug like this in 15 years of Java and C++ programming. One has to be really sloppy in order to insert bugs in such simple code.
It's not as uncommon as you might expect -- it happened to me before. Though of course, they do tend to get caught rather quickly. T -- The early bird gets the worm. Moral: ewww...
Jun 21 2014
prev sibling next sibling parent "Chris Cain" <zshazz gmail.com> writes:
On Saturday, 21 June 2014 at 21:47:00 UTC, SomeDude wrote:
 I've never seen a single instance of a bug like this in 15 years
 of Java and C++ programming. One has to be really sloppy in 
 order
 to insert bugs in such simple code.
Related: http://www.reddit.com/r/programming/comments/270orx/the_last_line_effect/
Jun 21 2014
prev sibling parent "bearophile" <bearophileHUGS lycos.com> writes:
SomeDude:

 I've never seen a single instance of a bug like this in 15 years
 of Java and C++ programming. One has to be really sloppy in 
 order to insert bugs in such simple code.
Do you mean bugs like this? class Foo { int x; this(int x_) { x = x; } } I have had some of them, found and fixed quickly. I think they are sufficiently common (unless you have an IDE that generates that boilerplate for you). Bye, bearophile
Jun 22 2014
prev sibling next sibling parent reply Shammah Chancellor <anonymous coward.com> writes:
On 2014-06-21 17:17:56 +0000, Suliman said:

 Dart and few others modern languages support short declaration 
 constructor with parameter:
 
 class Person {
    String name;
 
    Person(String name) {
      this.name = name;
    }
 }
 
 // Shorter alternative
 class Person {
    String name;
 
    // parameters prefixed by 'this.' will assign to
    // instance variables automatically
    Person(this.name);
 }
 
 it's there any DIP for adding this future to D?
I can't support this proposal. Adds more syntax to a language that is already becoming cramped. I also don't see the purpose of having simple constructors like this? Are you going to add (n choose k) simple constructors to a class? I could get behind field initializer new Person() {name: "Bob"}; -Shammah
Jun 22 2014
parent reply "Kapps" <opantm2+spam gmail.com> writes:
On Sunday, 22 June 2014 at 11:50:31 UTC, Shammah Chancellor wrote:
 I can't support this proposal.   Adds more syntax to a language 
 that is already becoming cramped.  I also don't see the purpose 
 of having simple constructors like this?  Are you going to add 
 (n choose k) simple constructors to a class?   I could get 

 syntax we have for structures.

 new Person() {name: "Bob"};

 -Shammah
In theory, 'with' could be used for that if it returned the expression passed in: auto a = with(new Person()) { Name = "Bob"; Age = 27; }
Jun 22 2014
next sibling parent simendsjo <simendsjo gmail.com> writes:
On 06/22/2014 09:58 PM, Kapps wrote:
 On Sunday, 22 June 2014 at 11:50:31 UTC, Shammah Chancellor wrote:
 I can't support this proposal.   Adds more syntax to a language that
 is already becoming cramped.  I also don't see the purpose of having
 simple constructors like this?  Are you going to add (n choose k)
 simple constructors to a class?   I could get behind field initializer


 new Person() {name: "Bob"};

 -Shammah
In theory, 'with' could be used for that if it returned the expression passed in: auto a = with(new Person()) { Name = "Bob"; Age = 27; }
I had to test that syntax. I got it working with these versions: C c; with(c = new C()) { i = 10; } C d = new C(); with(d) { i = 20; } much appreciated. Or if with could create objects in outer scope like this with(auto c = new C()) { i = 30; }
Jun 23 2014
prev sibling parent "Chris Cain" <zshazz gmail.com> writes:
On Sunday, 22 June 2014 at 19:58:38 UTC, Kapps wrote:
 In theory, 'with' could be used for that if it returned the 
 expression passed in:
 auto a = with(new Person()) {
     Name = "Bob";
     Age = 27;
 }
You forgot a semicolon after the } ... or at least that's what I think it would need. I kinda really like this. You could also do things like: auto bob = with(PersonBuilder()) { name = "Bob"; age = 27; }.build(); with a `with` expression, to make it transactional instead. This could do a lot of neat things, for sure.
Jun 23 2014
prev sibling next sibling parent reply "Grogan" <Grogan OD.fr> writes:
On Saturday, 21 June 2014 at 17:17:57 UTC, Suliman wrote:
 Dart and few others modern languages support short declaration 
 constructor with parameter:

 class Person {
   String name;

   Person(String name) {
     this.name = name;
   }
 }

 // Shorter alternative
 class Person {
   String name;

   // parameters prefixed by 'this.' will assign to
   // instance variables automatically
   Person(this.name);
 }

 it's there any DIP for adding this future to D?
If name is a property with getter and/setter: stack overflow ;). It's like the old "with" debate.
Jun 23 2014
parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Mon, 23 Jun 2014 17:38:53 -0400, Grogan <Grogan od.fr> wrote:

 On Saturday, 21 June 2014 at 17:17:57 UTC, Suliman wrote:
 Dart and few others modern languages support short declaration  
 constructor with parameter:

 class Person {
   String name;

   Person(String name) {
     this.name = name;
   }
 }

 // Shorter alternative
 class Person {
   String name;

   // parameters prefixed by 'this.' will assign to
   // instance variables automatically
   Person(this.name);
 }

 it's there any DIP for adding this future to D?
If name is a property with getter and/setter: stack overflow ;).
How so? -Steve
Jun 23 2014
prev sibling parent reply "AsmMan" <lol.themask gmail.com> writes:
On Saturday, 21 June 2014 at 17:17:57 UTC, Suliman wrote:
 Dart and few others modern languages support short declaration 
 constructor with parameter:

 class Person {
   String name;

   Person(String name) {
     this.name = name;
   }
 }

 // Shorter alternative
 class Person {
   String name;

   // parameters prefixed by 'this.' will assign to
   // instance variables automatically
   Person(this.name);
 }

 it's there any DIP for adding this future to D?
I don't agree with this feature. It's ugly. At first look at you have no idea what it does mean (even if you know have already some base about programming languages). I think this type of feature would lead D to became a type of C++. See C++ foo(int x) : x(x) {} is just too ugly either. I don't know if there are someone that share same feel.
Jun 23 2014
next sibling parent "Tofu Ninja" <emmons0 purdue.edu> writes:
On Tuesday, 24 June 2014 at 03:19:58 UTC, AsmMan wrote:
 See C++ foo(int x) : x(x) {} is just too ugly either.  I don't
 know if there are someone that share same feel.
I agree, we don't need more syntax sugar.
Jun 23 2014
prev sibling parent reply "AsmMan" <lol.themask gmail.com> writes:
On Tuesday, 24 June 2014 at 03:19:58 UTC, AsmMan wrote:
 On Saturday, 21 June 2014 at 17:17:57 UTC, Suliman wrote:
 Dart and few others modern languages support short declaration 
 constructor with parameter:

 class Person {
  String name;

  Person(String name) {
    this.name = name;
  }
 }

 // Shorter alternative
 class Person {
  String name;

  // parameters prefixed by 'this.' will assign to
  // instance variables automatically
  Person(this.name);
 }

 it's there any DIP for adding this future to D?
I don't agree with this feature. It's ugly. At first look at you have no idea what it does mean (even if you know have already some base about programming languages). I think this type of feature would lead D to became a type of C++. See C++ foo(int x) : x(x) {} is just too ugly either. I don't know if there are someone that share same feel.
"I think this type of feature would lead D to became a type of C++." I mean, C++ too complex and useless features. Like the constructor I've mentioned.
Jun 23 2014
parent "bearophile" <bearophileHUGS lycos.com> writes:
AsmMan:

 "I think this type of feature would lead D to became a type of 
 C++."

 I mean, C++ too complex and useless features. Like the 
 constructor I've mentioned.
If D reaches 1/10 of the diffusion of C++ we can call it a great success :-) I don't like tricky, over-engineered and bug-prone features (especially the ones that interact badly with other language features), but the feature discussed here seems able to reduce some mistakes. Bye, bearophile
Jun 30 2014