Readonly class members

Jarrett Billingsley kb3ctd2 at yahoo.com
Fri Sep 15 14:22:12 PDT 2006


"Georg Wrede" <georg.wrede at nospam.org> wrote in message 
news:4509605C.3040408 at nospam.org...

>> class Obj {
>>     void f() {
>>         int m_size = 10;  //ok
>>     }
>>
>>     readonly int m_size;
>> }
>>
>> void func() {
>>     Obj o = new Obj;
>>     int v;
>>
>>     v = o.m_size;  //ok
>>     o.m_size = 5;  //error
>> }

> Hmm. IMHO, this would be very easy to implement in the compiler. It would 
> make class definitions clearer, and in many cases would lessen the need to 
> write code.

class Obj
{
    void f()
    {
        int mSize = 10;
    }

    public int size()
    {
        return mSize;
    }

    private int mSize;
}

..

Obj o = new Obj;
o.f();
writefln(o.size);
o.size = 4; // illegal

Properties, broperties. 





More information about the Digitalmars-d mailing list