January 26, 2018 functions allowed to overload on const int vs int vs immutable int? + spec is not accurate | ||||
---|---|---|---|---|
| ||||
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 |
Copyright © 1999-2021 by the D Language Foundation