Can I make a variable public and readonly (outside where was declared) at same time?
via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Fri Sep 26 11:14:39 PDT 2014
On Friday, 26 September 2014 at 17:52:58 UTC, bearophile wrote:
> Marc Schütz:
>
>> Alternatively, you could create a union with a private and a
>> public member with the same types, but I wouldn't recommend
>> it. Besides, the members would need to have different names:
>>
>> class Foo {
>> union {
>> private int a;
>> public int b;
>> }
>> }
>
> You can call them a and a_. Why are you not recommending this
> solution? It looks cool (if it works).
Don't know, it feels hacky. And I really don't like the two
different names, what's the point if I have to distinguish the
two manually?
Now yet another way came to my mind:
struct PrivatelyWritableProperty(T) {
private T value_;
const(T) get() const { return value_; }
private void opAssign(T value) { value_ = value; }
alias get this;
}
class Foo {
PrivatelyWritableProperty!int a;
}
But this looks even more fragile; there are probably some
corner-cases where it doesn't work (for example, what happens
when someone copies the containing struct?).
More information about the Digitalmars-d-learn
mailing list