Static functions calls should only resolve to static functions

Salih Dincer salihdb at hotmail.com
Sat Oct 26 06:04:07 UTC 2024


On Monday, 21 October 2024 at 01:22:02 UTC, HuskyNator wrote:
> 
> ## 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.

I agree with your published suggestion and find it appropriate. 
Because only one of the following versions (s) is compiled. 
However, if your suggestion is implemented, version i will also 
be made to work. Thank you for sharing your intuitive idea.

```d
version = s;
struct S
{
   version(i) // onlineapp.d(29): Error:
   {// calling non-static function `foo` requires an instance of 
type `S`
     void foo(ubyte u) {}
     static void foo(T)(T i) {}
   }

   version(u) // onlineapp.d(30): Error:
   {// calling non-static function `foo` requires an instance of 
type `S`
     static void foo(ubyte u) {}
     void foo(T)(T i) {}
   }

   version(s)
   {// Works:
     static
     {
       void foo(ubyte u) {1.write;}
       void foo(T)(T i) {2.write;}
     }
   }
}

void main()
{
   ubyte arg;
   S.foo(arg); // prints 1
   S.foo(int.max); // prints 2

   S s;
   s.foo(arg); // prints 1
   s.foo(int.max);// prints 2
}
```

SDB at 79



More information about the dip.ideas mailing list