Immutable objects and constructor ?

chmike via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri May 20 13:30:22 PDT 2016


On Friday, 20 May 2016 at 17:35:01 UTC, Kagamin wrote:
> On Friday, 20 May 2016 at 16:09:54 UTC, chmike wrote:
>> But I now met another error in my main(). I can't assign the 
>> immutable object to a mutable reference.
>>
>> Info x1 = MyInfos.one;
>>
>> Is it possible to define a mutable reference to an immutable 
>> instance ? 
>
> Sort of possible with a library solution:
>
> import std.typecons;
> auto x1 = rebindable(MyInfos.one);

I'm a bit surprized that the language doesn't support this. We 
have immutable strings that can be assigned to different 
variables. Why couldn't we do the same with objects ?

This rebindable is not user friendly.
I really wish the user could write this

Info x = MyInfos.one;

I may achieve this if Info is defined as a struct with a single 
member defined as rebindable.




More information about the Digitalmars-d-learn mailing list