www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Adding syntacti sugar for simple "readonly" attribute ?

reply LunaticWare <exemple gmail.com> writes:
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
next sibling parent jmh530 <john.michael.hall gmail.com> writes:
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
prev sibling next sibling parent =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
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/DIPs
 so 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
prev sibling parent reply bauss <jj_1337 live.dk> writes:
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
parent reply Jacob Carlborg <doob me.com> writes:
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
parent reply bauss <jj_1337 live.dk> writes:
On Friday, 27 October 2017 at 06:49:47 UTC, Jacob Carlborg wrote:
 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.
Ahh yeah, that's true. I wasn't thinking that far
Oct 27 2017
parent Jacob Carlborg <doob me.com> writes:
On 2017-10-27 11:06, bauss wrote:

 Ahh yeah, that's true. I wasn't thinking that far
I 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