digitalmars.D - shorter alternative of constructor with parameter
- Suliman (16/16) Jun 21 2014 Dart and few others modern languages support short declaration
- bearophile (10/18) Jun 21 2014 This was already discussed once or more in past. Take a look in
- Xinok (5/21) Jun 21 2014 I'd prefer that we didn't crowd the language with minor shortcuts
- Suliman (2/3) Jun 21 2014 How not implementing this future will help to save syntactic
- Xinok (5/8) Jun 21 2014 I mean to use syntactic sugar sparingly, not for minor features
- Suliman (4/8) Jun 21 2014 Less code make code more easy to read. I do not think that such
- "Ola Fosheim =?UTF-8?B?R3LDuHN0YWQi?= (3/5) Jun 21 2014 Yep, it encourage tuples with named fields. Which makes code
- Xinok (5/13) Jun 22 2014 Part of my problem with this proposal is that its use case is
- Dicebot (6/9) Jun 21 2014 There is a limited amount of non-widespread syntax sugar you can
- Jonathan M Davis via Digitalmars-d (7/33) Jun 21 2014 Agreed. This would just add more stuff to the language that people would...
- bearophile (20/24) Jun 21 2014 Now there are languages with such idea, like TypeScript and
- Steven Schveighoffer (9/45) Jun 23 2014 Yeah, I don't think we save much with this. A mixin should be able to
- H. S. Teoh via Digitalmars-d (59/106) Jun 23 2014 [...]
- Gary Willoughby (6/10) Jun 23 2014 I agree please don't add shortcuts like this that really don't
- bearophile (6/7) Jun 23 2014 I don't think a template/mixin can replace that sufficiently
- Steven Schveighoffer (17/21) Jun 23 2014 The mixin does not have to generate a whole function, it can just genera...
- "Ola Fosheim =?UTF-8?B?R3LDuHN0YWQi?= (9/12) Jun 29 2014 And it is unreadable. "this.x" is very useful for creating new
- "Ola Fosheim =?UTF-8?B?R3LDuHN0YWQi?= (3/4) Jun 29 2014 I meant "tmp=b; a=tmp", of course… :-P
- Kapps (4/20) Jun 21 2014 Personally I'd definitely welcome this syntax. It's an extremely
- SomeDude (4/8) Jun 21 2014 I've never seen a single instance of a bug like this in 15 years
- H. S. Teoh via Digitalmars-d (6/15) Jun 21 2014 It's not as uncommon as you might expect -- it happened to me before.
- Chris Cain (3/7) Jun 21 2014 Related:
- bearophile (13/16) Jun 22 2014 Do you mean bugs like this?
- Shammah Chancellor (8/29) Jun 22 2014 I can't support this proposal. Adds more syntax to a language that is
- Kapps (7/15) Jun 22 2014 In theory, 'with' could be used for that if it returned the
- simendsjo (13/30) Jun 23 2014 I had to test that syntax. I got it working with these versions:
- Chris Cain (10/16) Jun 23 2014 You forgot a semicolon after the } ... or at least that's what I
- Grogan (3/19) Jun 23 2014 If name is a property with getter and/setter: stack overflow ;).
- Steven Schveighoffer (3/26) Jun 23 2014 How so?
- AsmMan (7/23) Jun 23 2014 I don't agree with this feature. It's ugly. At first look at you
- Tofu Ninja (2/4) Jun 23 2014 I agree, we don't need more syntax sugar.
- AsmMan (5/34) Jun 23 2014 "I think this type of feature would lead D to became a type of
- bearophile (9/13) Jun 30 2014 If D reaches 1/10 of the diffusion of C++ we can call it a great
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
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
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
and save syntactic sugar for more useful features.How not implementing this future will help to save syntactic sugar?
Jun 21 2014
On Saturday, 21 June 2014 at 18:57:25 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.and save syntactic sugar for more useful features.How not implementing this future will help to save syntactic sugar?
Jun 21 2014
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
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
On Sunday, 22 June 2014 at 06:27:52 UTC, Suliman wrote: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.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 22 2014
On Saturday, 21 June 2014 at 18:57:25 UTC, Suliman wrote: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.and save syntactic sugar for more useful features.How not implementing this future will help to save syntactic sugar?
Jun 21 2014
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: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 DavisDart 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
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
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: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. -SteveOn Saturday, 21 June 2014 at 17:17:57 UTC, Suliman wrote: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.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 23 2014
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:[...] 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 BrightOn Sat, 21 Jun 2014 18:50:21 +0000 Xinok via Digitalmars-d <digitalmars-d puremagic.com> wrote: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.On Saturday, 21 June 2014 at 17:17:57 UTC, Suliman wrote: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.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 23 2014
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
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
On Mon, 23 Jun 2014 14:20:32 -0400, bearophile <bearophileHUGS lycos.com> wrote:Gary Willoughby: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 } } -SteveAs 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.
Jun 23 2014
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
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
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
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
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: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...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
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
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
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
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"}; -ShammahIn 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
On 06/22/2014 09:58 PM, Kapps wrote:On Sunday, 22 June 2014 at 11:50:31 UTC, Shammah Chancellor wrote: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; }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"}; -ShammahIn theory, 'with' could be used for that if it returned the expression passed in: auto a = with(new Person()) { Name = "Bob"; Age = 27; }
Jun 23 2014
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
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
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:How so? -SteveDart 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 ;).
Jun 23 2014
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
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
On Tuesday, 24 June 2014 at 03:19:58 UTC, AsmMan wrote:On Saturday, 21 June 2014 at 17:17:57 UTC, Suliman wrote:"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.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
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