Overloading based on attributes - is it a good idea?

Ola Fosheim Grøstad ola.fosheim.grostad at gmail.com
Sat Jun 1 05:20:40 UTC 2019


On Friday, 31 May 2019 at 21:39:10 UTC, Manu wrote:
> A fun() { return A(); }
> B fun() { return B(); }
>
> fun(); // <- return value ignored
>
> ...?

In this instance it is a matter of "ideological" language 
semantics.

If the language "ideology" is spec-driven then compiler should be 
allowed to choose the one that does less work, given that they 
both are supposed to implement the same spec. The IDE can show 
which one is chosen, so I don't think it is as big a problem as 
people make it out to be (assuming you limit your imports).

If the language has error-as-return-value as an idiom, then it 
might require the caller to check all returned errors. In that 
case it should pick the function call  that throws if the caller 
does not use the returned value, and complain if both versions of 
"fun()" returns an error value.

Anyway, you can get around it by:

A _ = fun();    // _ is not used

or:

returns!A( fun() );  // wrapper that ensures the return type to 
be of A



More information about the Digitalmars-d mailing list