Why is my @pure function @system when placed in a struct?

Q. Schroll qs.il.paperinik at gmail.com
Wed Feb 27 17:23:21 UTC 2019


I have a template function `fImpl` I whish to instantiate 
manually using the new name `f`. Reason is simple: `f` should not 
be a template, but overloading it makes it easier that way.
Nothing's more simple in D:

     int fImpl(T)(T value) { return cast(int) value; }
     alias f = fImpl!int;
     alias f = fImpl!long;

It works perfectly used like that.

In my case, `T` isn't just a simple type, it's a delegate type. 
So it's rather like this:

     alias BaseDG = int delegate(ref int);
     int fImpl(DG : BaseDG)(scope DG callback)
     {
         // NB: this is @safe iff callback is @safe
         int x = 0;
         return callback(x);
     }

     alias myDG = int delegate(ref int) @safe;
     alias f = fImpl!myDG;

When I ask the compiler, if `f` is @safe, it tells me: Hurray, it 
is!

     pragma(msg, __traits(getFunctionAttributes, f)); // tells me: 
`f` is @safe

For whatever reason, when I put the code in a struct, the @safe 
testing line tells me, it's @system now.

     struct S
     {
         // static: // static or not does not matter

         alias BaseDG = int delegate(ref int);
         int fImpl(DG : BaseDG)(scope DG callback) { return 0; }

         alias myDG = int delegate(ref int) @system;
         alias f = fImpl!myDG;

         pragma(msg, __traits(getFunctionAttributes, f)); // tells 
me: `f` is @system
     }

I have no idea why. It is irrelevant if the function template is 
`static` or even does not call the callback.


More information about the Digitalmars-d-learn mailing list