Allowing "fall through" of attributes

Lodovico Giaretta via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Jul 19 10:10:35 PDT 2016


On Tuesday, 19 July 2016 at 17:05:55 UTC, Rufus Smith wrote:
> On Tuesday, 19 July 2016 at 16:59:48 UTC, Lodovico Giaretta 
> wrote:
>> On Tuesday, 19 July 2016 at 16:50:56 UTC, Rufus Smith wrote:
>>> On Tuesday, 19 July 2016 at 16:09:38 UTC, Lodovico Giaretta 
>>> wrote:
>>>> [...]
>>>
>>> But this doesn't create a function with all the attributes of 
>>> the original? Just one that has the same return type and 
>>> parameters. What if Fun is pure or extern(C) or some other 
>>> attributes? I'd like to create a function that is exactly the 
>>> same in all regards as the original.
>>
>> Sorry, I misunderstood your question.
>> With the method I showed you, if the function is @safe, pure, 
>> @nogc or nothrow, foo will infer those attributes. But only if 
>> the operations you do in foo (apart from calling bar) are 
>> themselves @safe, pure, @nogc or nothrow.
>> For other things, like extern(C), I don't think there's a 
>> simple solution; but I'm not an expert, so I hope someone else 
>> will give you a better answer.
>
> What is strange is I cannot even pass an extern(C) function to 
> foo.
>
> void foo(R, A...)(R function(A) bar);
>
> extern(C) void bar();
>
> foo(&bar)
>
> fails. Remove extern and it passes. I have not figured out how 
> to allow for extern(C) functions to be passed.

That's because an extern function must be called with a different 
code. So it cannot be cast to a non-extern(C) function pointer, 
which is what your foo accepts. If you follow my advice, and make 
the entire function type a parameter of foo, then foo will at 
least accept your extern(C) function, but it will not be 
extern(C) itself.


More information about the Digitalmars-d-learn mailing list