How to get an inout constructor working with a template wrapper

aliak something at something.com
Sat Jul 28 22:25:23 UTC 2018


On Friday, 27 July 2018 at 14:34:54 UTC, Steven Schveighoffer 
wrote:
> The problem here is that inout(immutable(int)) is equivalent to 
> immutable(int).
>
> That is, all flavors of mutability are equivalent to 
> immutable(int):
>
> /*mutable*/(immutable(int)) => immutable(int)
>       const(immutable(int)) => immutable(int)
>   immutable(immutable(int)) => immutable(int)
>
> So the compiler really looks at your wrap instantiation like 
> this;
>
> inout(W!(immutable(int))) wrap(immutable(int) t)

Ah ok, so the compiler remove inout behind me back here? (And 
then tells me it needs to be there? :p)

>
> which triggers the (really bad) message.
>
> I'd ask, why are you even worrying about explicit 
> instantiation? Why not just wrap(3)?

Just because I don't see why it should not work really. Why not 
allow wrap!(immutable int)(3)?

>
> or (if you really want to test it) wrap(immutable(int)(3))?
>
>> 
>> To make it compile successfully you can either:
>> 
>> 1) Chance immutable to const, then it works for some reason.
>
> Because immutable(const(int)) => immutable(int), so the 
> compiler can't remove the inout behind your back.
>
>> 2) Change the line to: "auto si = wrap(cast(immutable int)3);" 
>> - i.e. do not explicitly provide type information.
>
> Yep, do this :)
>
> Note that the point of inout is 2-fold:
>
> 1. reduce template instantiations. In fact, wrap!int works for 
> const, mutable and immutable int.
> 2. ENSURE that the data isn't modified, even in the case of 
> mutable parameters.

Thanks for the explanations! For some reason it's hard to get it 
all to *just work* right now without the template this. But it's 
probably some minor detail I'm just overlooking...

>
> -Steve




More information about the Digitalmars-d-learn mailing list