How to get an inout constructor working with a template wrapper

Timoses timosesu at gmail.com
Mon Jul 23 14:46:32 UTC 2018


On Monday, 23 July 2018 at 12:02:58 UTC, aliak wrote:
>
> Thank you Ali! That helped :) I've gotten most of it sorted out 
> now, and the factory wrap is definitely the way to go, it also 
> turned out that inout(T) and inout T (so inout without parens) 
> was surprisingly different (maybe it's a bug? - to test you can 
> remove the parens around U on line 3 in this sample: 
> https://run.dlang.io/is/gd5oxW
>
> Also over there, line 24:
>
> auto si = wrap!(immutable int)(3);

Both of these seem to work (as you pointed out)

     // immutable(W!int)
     auto si = wrap!(int)(cast(immutable)3); // or 
wrap(cast(immutable)3);
     // W!(immutable(int))
     auto si2 = W!(immutable int)(3);

>
> seems to be giving problems. Any ideas there? Error is:
>
> onlineapp.d(8): Error: inout on return means inout must be on a 
> parameter as well for pure nothrow @nogc @safe 
> inout(W!(immutable(int)))(immutable(int) t)
> onlineapp.d(23): Error: template instance 
> `onlineapp.wrap!(immutable(int))` error instantiating
>
> To make it compile successfully you can either:
>
> 1) Chance immutable to const, then it works for some reason.
> 2) Change the line to: "auto si = wrap(cast(immutable int)3);" 
> - i.e. do not explicitly provide type information.

I don't know why

     wrap!(immutable int)(3);

is not working. The error message

     "Error: inout on return means inout must be on a parameter as 
well for pure nothrow @nogc @safe 
inout(W!(immutable(int)))(return immutable(int) t)"

sounds very odd and not at all helpful, at least regarding that 
removing immutable from the template argument works.

>
> Cheers,
> - Ali

The depths of D. Why does the following only work with "return 
ref"?

     struct W(T) {
         T val;
         this(U : T)(auto ref inout U val) inout {
             pragma(msg, typeof(val));
             this.val = val;
         }
     }

     // Fails without "return ref" (escaping t warning...)
     auto wrap(T)(return ref inout T t) {
         return inout W!T(t);
     }

     class C {}

     void main() {
         immutable C ci = new immutable C;
         auto i = wrap(im);
         pragma(msg, typeof(i));
     }


More information about the Digitalmars-d-learn mailing list