Can I make a variable public and readonly (outside where was declared) at same time?
    Steven Schveighoffer via Digitalmars-d-learn 
    digitalmars-d-learn at puremagic.com
       
    Fri Sep 26 11:18:44 PDT 2014
    
    
  
On 9/26/14 1:36 PM, "Marc =?UTF-8?B?U2Now7x0eiI=?= <schuetzm at gmx.net>" 
wrote:
> 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;
>          }
>      }
Hm.. that doesn't provide readonly access to either a or b.
But it gave me an idea:
class Foo {
    union {
       private int _a;
       public const int a;
    }
    void setA(int x) { _a = x; }
}
Hot damn! It works too :) Can't access _a from outside the module, can 
access a, but can't write it (even from within Foo). It's like an 
auto-inlined property function.
I don't know how it would affect the optimizer, or the GC scanner. 
Unions are ugly things...
-Steve
    
    
More information about the Digitalmars-d-learn
mailing list