[Issue 3345] Static and nonstatic methods with the same name should be allowed

d-bugmail at puremagic.com d-bugmail at puremagic.com
Wed Jul 28 15:18:11 UTC 2021


https://issues.dlang.org/show_bug.cgi?id=3345

RazvanN <razvan.nitu1305 at gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |razvan.nitu1305 at gmail.com

--- Comment #7 from RazvanN <razvan.nitu1305 at gmail.com> ---

(In reply to Andrei Alexandrescu from comment #0)
> Consider:
> 
> class Widget {
>     int fun() { return 1; }
>     static int fun() { return 2; }
> }
> 
> void main() {
>     writeln(Widget.fun());       // should print 2
>     writeln((new Widget).fun()); // should print 1
> }
> 
> This should work. Otherwise there is no way to explain how .classinfo works
> in terms of D facilities.

Currently, the staticness of a function is not considered when doing overload
resolution. What the compiler does is simply check whether the function needs a
this pointer or not **after** it has resolved it. This is an elegant solution
and it greatly simplifies the implementation since you don't have to take
staticness into account when matching the overloads. Of course, the trade off
is that overloading static and non-static member functions does not work well.

I would argue that mixing static and non-static functions in the same overload
set is bad practice and therefore should be discouraged. Since we can call
static member functions on both instances and non-instances, why would we need
something like this bug report proposes?

I suggest that we dissallow overloading static and non-static functions. It
makes for an easy implementation and clear spec at the cost of having the user
rename a function.

--


More information about the Digitalmars-d-bugs mailing list