Thread: function overloading / ambigous functions

function overloading / ambigous functions

From
Kyle
Date:
I'm having some difficulty with overloading functions.  I've created a
function that normally takes int2's.  This works fine, but then I
decided to overload it with a version that takes float8 as input.  The
problem arises because the float8 version seems to be called every
time, unless an argument is explicitly cast to int2.  Given:

CREATE FUNCTION my_search(int2, int2, int2, int2)
  RETURNS int4 AS '/home/kaf/lib/my_search.so',
    'my_search' LANGUAGE 'c';

CREATE FUNCTION my_search(float8, float8, float8, float8)
  RETURNS int4 AS '/home/kaf/lib/my_search.so',
    'my_search_float8' LANGUAGE 'c';

select my_search(7879,3727,3061,2502);

this uses the float8 implementation.  I'd expect the int2
implementation to be used since none of the arguments contain floats.
Is this the expected behavior?  Of course, if I explicitly cast any
argument to an int2, the int2 function is called.

Thanks,
-Kyle


Re: function overloading / ambigous functions

From
Stephan Szabo
Date:
On Mon, 5 Mar 2001, Kyle wrote:

> I'm having some difficulty with overloading functions.  I've created a
> function that normally takes int2's.  This works fine, but then I
> decided to overload it with a version that takes float8 as input.  The
> problem arises because the float8 version seems to be called every
> time, unless an argument is explicitly cast to int2.  Given:
>
> CREATE FUNCTION my_search(int2, int2, int2, int2)
>   RETURNS int4 AS '/home/kaf/lib/my_search.so',
>     'my_search' LANGUAGE 'c';
>
> CREATE FUNCTION my_search(float8, float8, float8, float8)
>   RETURNS int4 AS '/home/kaf/lib/my_search.so',
>     'my_search_float8' LANGUAGE 'c';
>
> select my_search(7879,3727,3061,2502);
>
> this uses the float8 implementation.  I'd expect the int2
> implementation to be used since none of the arguments contain floats.
> Is this the expected behavior?  Of course, if I explicitly cast any
> argument to an int2, the int2 function is called.

The constants are getting treated as int4 by default and are probably
being promoted towards float8 rather than down to int2.  There's been
talk to redo stuff on the type system for numbers, but I don't think
stuff that would handle this is done.  I'd suggest making the function
take int4's for now.



Re: function overloading / ambigous functions

From
Tom Lane
Date:
Kyle <kaf@nwlink.com> writes:
> CREATE FUNCTION my_search(int2, int2, int2, int2)
>   RETURNS int4 AS '/home/kaf/lib/my_search.so',
>     'my_search' LANGUAGE 'c';

> CREATE FUNCTION my_search(float8, float8, float8, float8)
>   RETURNS int4 AS '/home/kaf/lib/my_search.so',
>     'my_search_float8' LANGUAGE 'c';

> select my_search(7879,3727,3061,2502);

> this uses the float8 implementation.  I'd expect the int2
> implementation to be used since none of the arguments contain floats.

Nope.  Those numbers are int4 by default, not int2, and it won't coerce
downwards without guidance.

Consider declaring your integer version to take int4s instead of int2s,
and doing the narrowing internally if you really want it.

            regards, tom lane