Overloading based on attributes - is it a good idea?

Exil Exil at gmall.com
Thu May 30 13:58:20 UTC 2019


On Thursday, 30 May 2019 at 05:24:21 UTC, Manu wrote:
> On Wed, May 29, 2019 at 6:55 AM Mike Franklin via Digitalmars-d 
> <digitalmars-d at puremagic.com> wrote:
>>
>> On Tuesday, 28 May 2019 at 16:08:38 UTC, Andrei Alexandrescu 
>> wrote:
>> > int fun(int) pure;
>> > int fun(int);
>> >
>> > pure int gun(int x)
>> > {
>> >    return fun(x);
>> > }
>>
>> I think it really depends on the attribute.  I haven't thought 
>> too much about the other attributes, but for `pure` I don't 
>> see the use case.  If you can make the implementation of 
>> `fun(int)` pure, why would you need an additional impure 
>> implementation?
>
> Right. nothrow is the only one that makes any sense at all to 
> me, I've thought that overloading on nothrow might be useful 
> once or twice. Otherwise... pure; like you say, @nogc; 
> signature is always different anyway, @safe; makes no sense...

I don't think it is that useful. Consider if you did do the 
following:

     void foo() nothrow;
     void foo();

     void bar() nothrow
     {
          foo(); // ok useful calls foo() nothrow
     }

     void ree()
     {
         foo(); // ambiguous can call both, should be an error
     }


It can call either function if the callee isn't nothrow. I don't 
think it should implicitly call the function without the nothrow 
either. It really varies on the situation which one should be 
called, you may want the variant that doesn't throw an exception. 
It would be surprising if you have a function defined as nothrow, 
then add an overload without the nothrow and now it is now 
calling a different overload in functions without nothrow. At 
which point this isn't all that useful, it will work as expected 
in nothrow functions but then you will have to explicitly choose 
which one to use otherwise. Which is basically the situation now 
with nothrow, you just have to use a different function name.




More information about the Digitalmars-d mailing list