www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Value Range Propagation with generic and specialized function

reply "Wagner Macedo" <wagnermacedo.ufs gmail.com> writes:
Hello,

Firstly, I'm not an actual D programmer. I'm doing a college
research about D language.

My point here is that I faced the following situation.

With the code above

      void main() {
          fun(1f);
          fun(1);
          fun(1L);
          fun!(long)(1L);
      }

      void fun(T)(T i) {
          writeln(typeid(T));
      }

      void fun(int i) {
          writeln("special => int");
      }

I get the output

      float
      special => int
      special => int
      long

that is, even if I call fun(1L), with long literal, the VRP
decide to use specialized fun(int). Due this, it's needed to
specify that I need to use the generic function (losing
readability).

Said this, I wanted to know if this is a bug or an expected
behavior.
May 22 2014
next sibling parent Etienne <etcimon gmail.com> writes:
On 2014-05-22 7:37 PM, Wagner Macedo wrote:
           fun(1);
           fun(1L);
           fun!(long)(1L);

Non-template functions are preferred over template, they have priority even when it's done with implicit conversion. This is the case for C++ as well. http://stackoverflow.com/questions/10291405/priority-between-normal-function-and-template-function
May 22 2014
prev sibling next sibling parent "bearophile" <bearophileHUGS lycos.com> writes:
Etienne:

 Non-template functions are preferred over template, they have 
 priority even when it's done with implicit conversion.

 This is the case for C++ as well.

Perhaps Rust doesn't have function overloading? Bye, bearophile
May 22 2014
prev sibling parent "Wagner Macedo" <wagnermacedo.ufs gmail.com> writes:
On Friday, 23 May 2014 at 00:04:00 UTC, Etienne wrote:
 Non-template functions are preferred over template, they have 
 priority even when it's done with implicit conversion.

 This is the case for C++ as well.

 http://stackoverflow.com/questions/10291405/priority-between-normal-function-and-template-function

Understood. Then, it's expected. Thank you!
May 22 2014