functions allowed to overload on const int vs int vs immutable int? + spec is not accurate

timotheecour timothee.cour2 at gmail.com
Fri Jan 26 19:45:54 UTC 2018


this compiles, but equivalent in C++ (const int vs int) would 
give a
compile error (error: redefinition of 'fun'); what's the 
rationale for
allowing these overloads?

```
void fun(int src){ writeln2(); }
void fun(immutable int src){ writeln2(); }
void fun(const int src){ writeln2(); }

void main(){
   {int src=0; fun(src);} // matches fun(int)
   {immutable int src=0; fun(src);} // matches fun(immutable int)
   {const int src=0; fun(src);} // matches fun(const int)
}
```

The spec does mention `match with conversion to const` taking
precedence over `exact match` however this isn't precise: the
following would be more precise instead (at least according to 
snippet
above):
`match with conversion to/from const/immutable/mutable`

https://dlang.org/spec/function.html#function-overloading

Functions are overloaded based on how well the arguments to a 
function
can match up with the parameters. The function with the best 
match is
selected. The levels of matching are:

no match
match with implicit conversions
match with conversion to const
exact match

NOTE: this was one of the root causes of the bug I just fixed in 
https://github.com/msoucy/dproto/pull/131: "For sint32/sint64, 
this was related to an overload never being called because of dmd 
overload resolution rules regarding const vs mutable"

NOTE: somehow this msg didn't appear on forum after I had sent it 
via gmail 1 hour ago; this usually works, not sure why it didn't 
work this time; apologies if msg appears twice because of that 
but seems like a forum bug.



More information about the Digitalmars-d mailing list