immutable / inout / pure headaches

Timoses timosesu at gmail.com
Fri Jul 6 15:22:48 UTC 2018


On Friday, 6 July 2018 at 14:28:39 UTC, Steven Schveighoffer 
wrote:
> inout is not a compile-time wildcard, it's a runtime one. So it 
> doesn't know how to convert an immutable to an inout. 
> Essentially, inside this function, the compiler has no idea 
> whether the real thing is an immutable, const, mutable, etc.
>
> The fix is simple, replace both your constructors with one 
> inout constructor:
>
> this(inout(S) t) inout { this.s = t; }

Slowly getting acquainted to inout... Feels like magic.

> And it will work for everything.
>
> One word of caution though -- inout is viral (just like 
> immutable). Everything you use has to support it, or it breaks 
> down.

"viral" is very fitting. Throw in pure and I quickly reach the 
bottom of my program hitting a library function I used which is 
not pure.

I never really used 'pure' and just now found a use case with 
immutable [1], i.e. to return unique objects from functions which 
can be assigned to a mutable or immutable reference.
What other "use cases" or reasons to use 'pure' are there (aside 
from compiler optimizations)?

[1]: 
https://forum.dlang.org/post/nmcnuenazaghjlxodlwz@forum.dlang.org


More information about the Digitalmars-d-learn mailing list