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.