Lazily evaluated property pointing to read only object
Basile B. via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sat Sep 24 04:51:56 PDT 2016
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.
More information about the Digitalmars-d-learn
mailing list