Is this correct?

Timon Gehr timon.gehr at gmx.ch
Thu Feb 6 23:24:37 UTC 2020


On 07.02.20 00:05, Jonathan Marler wrote:
> The named arguments DIP got to question what this example would do:
> 
> --- foolib.d
> import std.stdio;
> void foo(string s) { writefln("foo string"); }
> 
> 
> --- main.d
> import std.stdio;
> import foolib;
> void foo(const(char)[] s) { writefln("foo const"); }
> void main()
> {
>     foo("hello");
> }
> 
> 
> This prints "foo const".  And if you move the function overload from 
> foolib.d to main.d, it will print "foo string".  Is this the expected 
> behavior?
> 

Yes. The justification for this is that otherwise foolib could silently 
hijack your main.d by introducing a new function "foo". If you want to 
overload against the library function, you can use an alias:

import std.stdio;
import foolib;
alias foo=foolib.foo;
void foo(const(char)[] s) { writefln("foo const"); }
void main(){
    foo("hello"); // foo string
}


More information about the Digitalmars-d mailing list