Lazily evaluated property pointing to read only object

vit via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Sep 27 12:57:59 PDT 2016


On Saturday, 24 September 2016 at 11:51:56 UTC, Basile B. wrote:
> On Saturday, 24 September 2016 at 10:59:50 UTC, mikey wrote:
>> On Saturday, 24 September 2016 at 10:16:34 UTC, Basile B. 
>> wrote:
>>> You don't need to cast, from "mutable" to "const" is implicit:
>>> https://dlang.org/spec/const3.html#implicit_conversions
>>
>> Ok, but using const would be an accepted way of doing this? 
>> The options I could see were to have "_o" as a const or 
>> immutable type and just create a const on the first call to 
>> the lazily evaluated property, or to do what I did and have 
>> "_o" as a non-const and then convert it to cost on the way 
>> out. However
>>
>> To store it as const I guess I'd have to make it a non-const 
>> pointer to a const object, and is that not kind of what 
>> immutable is?
>
> Yes, the problem is that if you want to create a true 
> const(Object) (with const part of the type) you have to 
> initialize it in a constructor (so no lazyness). It indeed 
> looks like the immutable mechanism. I don't know **exactly** 
> why but I guess that's a special case for classes since there's 
> no other way to initialize them.
>
> Finally, with the property your object is seen as const(Object) 
> outside. The only difference is inside ConstProp.

use Rebindable:


class Obj {
         string _s;

         this()pure{
             this("");
         }
         this(string s)pure{
             _s = s;
         }

         @property string desc() const { return _s; }
         @property void desc(string s) { _s = s; }

         override string toString() const {
             return _s;
         }
     }

class ConstProp {
     import std.typecons : Rebindable;
     Rebindable!(immutable Obj) _o;
     string _s;

     this(string s) {
         _s = s;
     }

     immutable(Obj) lazily(){
         if (_o is null) {
             _o = new Obj("working " ~ _s);
         }
         return _o;
     }
}


More information about the Digitalmars-d-learn mailing list