Static functions calls should only resolve to static functions

HuskyNator huskynator at protonmail.ch
Mon Oct 21 01:22:02 UTC 2024


Currently, the combining of static and non-static function 
overloads can cause static overloads to be ignored, even when 
these are the _only_ fit.

I have been told this is simply the way it is. Although I would 
consider this a bug. It's even slightly problematic for my 
`static OpDispatch` code (workarounds always exist ...).
Given the self-contradicting documentation & differing views, I'm 
here suggesting it as an improvement. (If it _is_ to be 
considered a bug I would love to hear it)

## Code Example:
```d
import std;

class A {
     void foo(int a, bool b) {
         writeln("foo");
     }
     static void foo(T...)(T args) {
         writeln("static foo");
     }
}

void main(){
      A.foo(1,true); // calling non-static function `foo` requires 
an instance of type `A`
}
```

---

## Documentation:
(https://dlang.org/spec/function.html#function-overloading)
> 3. Each argument (including any **this** reference) is compared 
> against the function's corresponding parameter to determine the 
> match level for that argument. The match level for a function 
> is the worst match level of each of its arguments.

Thus one would expect `this` (or an absence of) to be considered 
during function resolution; `static` calls should be resolved to 
static functions.

> 8. A static member function can be overloaded with a member 
> function. The struct, class or union of the static member 
> function is inferred from the type of the this argument.

The intent in this sentence seems to be to indicate non-static 
calls can drop their implicit `this` reference. Though the 
sentence is very confusing.

The examples contained within the documentation meanwhile 
describe the current state of things. (No wonder, as they're 
unittests and need to pass)

---

## Suggestion / Expectation:

Given static calls are not using an implicit `this` reference, 
and are explicitly bound to using static functions, I suggest 
their calls should only ever resolve to static functions. The 
code above should then compile and print "static foo".

This would not break anything, and would make the overloading 
much more intuitive.


More information about the dip.ideas mailing list