digitalmars.D - Adding syntacti sugar for simple "readonly" attribute ?
- LunaticWare (35/35) Oct 26 2017 Hello everyone i am new to the D community and i really enjoy
- jmh530 (28/29) Oct 26 2017 You can use string mixins.
- =?UTF-8?Q?Ali_=c3=87ehreli?= (12/14) Oct 26 2017 Improvement proposals are handled through DIPs here:
- bauss (18/54) Oct 26 2017 The first example would not equal the second, because you could
- Jacob Carlborg (6/20) Oct 26 2017 That only works for primitive types. For anything else (like a class or
- bauss (2/20) Oct 27 2017 Ahh yeah, that's true. I wasn't thinking that far
- Jacob Carlborg (5/6) Oct 27 2017 I think head const [1] is what he's looking for. Similar to "final" in J...
Hello everyone i am new to the D community and i really enjoy programming in D, i haven't done anything significant so far. but being a very lazy person, when writing a bit of code i noticed that maybe for such a simple thing we could have a shorter syntax. i don't know if this is the correct way to suggest enhancement to D, and i am sorry if this is already in the language. so maybe we could add syntactic sugar for "readonly" attributes. here is simple example, where the following code --- class Foo { readonly int bar = 12; // or maybe " directread" ? this(string baz) { this.bar = baz; } } --- would be the same as --- class Foo { private string bar_; this(string baz) { this.bar_ = baz; } property string bar() { return this.bar_; } }
Oct 26 2017
On Thursday, 26 October 2017 at 21:19:28 UTC, LunaticWare wrote:[snip]You can use string mixins. template GenGetterSetter(string Type, string Name) { const char[] GenGetterSetter = " private " ~ Type ~ " " ~ Name ~ "_;\n" ~ " this(" ~ Type ~ " x)\n" ~ " {\n" ~ " " ~ Name ~ "_ = x;\n" ~ " }\n" ~ " property " ~ Type ~ " " ~ Name ~ "()\n" ~ " {\n" ~ " return " ~ Name ~ "_;\n" ~ " }"; } class Foo { mixin(GenGetterSetter!("string", "bar")); } void main() { import std.stdio : writeln; Foo foo = new Foo("bar"); writeln(foo.bar); }
Oct 26 2017
On 10/26/2017 02:19 PM, LunaticWare wrote:i don't know if this is the correct way to suggest enhancement to D,Improvement proposals are handled through DIPs here: https://github.com/dlang/DIPsso maybe we could add syntactic sugar for "readonly" attributes.There is the following project that comes close: http://forum.dlang.org/thread/zdcrkrktfsmvghmidamf forum.dlang.org class WithAccessors { Read Write private int num_; mixin(GenerateFieldAccessors); } Ali
Oct 26 2017
On Thursday, 26 October 2017 at 21:19:28 UTC, LunaticWare wrote:Hello everyone i am new to the D community and i really enjoy programming in D, i haven't done anything significant so far. but being a very lazy person, when writing a bit of code i noticed that maybe for such a simple thing we could have a shorter syntax. i don't know if this is the correct way to suggest enhancement to D, and i am sorry if this is already in the language. so maybe we could add syntactic sugar for "readonly" attributes. here is simple example, where the following code --- class Foo { readonly int bar = 12; // or maybe " directread" ? this(string baz) { this.bar = baz; } } --- would be the same as --- class Foo { private string bar_; this(string baz) { this.bar_ = baz; } property string bar() { return this.bar_; } }The first example would not equal the second, because you could set bar from anywhere within the module. Immutable will already do your behavior. class Foo { immutable string bar; this(string baz) { bar = baz; } } ... auto foo = new Foo("hello"); foo.bar ~= " World!"; // Error. string bar = foo.bar; // Okay. bar ~= " World!"; // Okay, because "bar" is not immutable, nor is it referring to foo.bar.
Oct 26 2017
On 2017-10-27 01:04, bauss wrote:The first example would not equal the second, because you could set bar from anywhere within the module. Immutable will already do your behavior. class Foo { immutable string bar; this(string baz) { bar = baz; } }That only works for primitive types. For anything else (like a class or struct) you won't be able to modify the internal state. While with the with the initial example you can. -- /Jacob Carlborg
Oct 26 2017
On Friday, 27 October 2017 at 06:49:47 UTC, Jacob Carlborg wrote:On 2017-10-27 01:04, bauss wrote:Ahh yeah, that's true. I wasn't thinking that farThe first example would not equal the second, because you could set bar from anywhere within the module. Immutable will already do your behavior. class Foo { immutable string bar; this(string baz) { bar = baz; } }That only works for primitive types. For anything else (like a class or struct) you won't be able to modify the internal state. While with the with the initial example you can.
Oct 27 2017
On 2017-10-27 11:06, bauss wrote:Ahh yeah, that's true. I wasn't thinking that farI think head const [1] is what he's looking for. Similar to "final" in Java. [1] https://dlang.org/const-faq.html#head-const -- /Jacob Carlborg
Oct 27 2017