getting rid of immutable (or const)

Andrew Edwards edwards.ac at gmail.com
Thu Sep 5 08:28:25 UTC 2019


On Thursday, 5 September 2019 at 08:16:08 UTC, Daniel Kozak wrote:
> On Thu, Sep 5, 2019 at 9:55 AM berni via Digitalmars-d-learn 
> <digitalmars-d-learn at puremagic.com> wrote:
>>
>> I still struggle with the concept of immutable and const:
>>
>> > import std.stdio;
>> >
>> > void main()
>> > {
>> >     auto p = Point(3);
>> >     auto q = p.x;
>> >     writeln(typeof(q).stringof);
>> > }
>> >
>> > struct Point
>> > {
>> >     @property immutable long x;
>> > }
>>
>> The type of q is immutable(long). But I need a mutable q. I 
>> found
>> two ways:
>>
>> a) long q = p.x;
>> b) auto q = cast(long)p.x;
>>
>> Either way I've to specify the type "long" which I dislike 
>> (here it's not a real burdon, but with more complicated types 
>> it might be). Is there a way, to make q mutable without having 
>> to write the type explicitly?
>
> in this case you can just use:
>
> auto q = cast()p.x;

or:

auto q = p.x + 0;


More information about the Digitalmars-d-learn mailing list