function overloading

Started by Robert Greimelover 20 years ago3 messagesgeneral
Jump to latest
#1Robert Greimel
greimel@ing.iac.es

Hi,

I have a question regarding function overloading:

assume that you have a function that takes several numeric arguments -
lets for example say 4 arguments: f(a,b,c,d)

Now further assume that internally in the function the first thing you
do is to convert the arguments to double precision, do your calculations
and return the result always in double precision.

In order to allow for all combinations of numeric
(int2,int4,int8,float4,float8) inputs one would now have to define and
write 5^4 = 625 functions !!!!!!!!!!!!!!!

Apart from forcing the function user to use casts, is there any other
way to avoid this madness of writing 625 functions ?

Thanks

Robert

#2Tom Lane
tgl@sss.pgh.pa.us
In reply to: Robert Greimel (#1)
Re: function overloading

Robert Greimel <greimel@ing.iac.es> writes:

I have a question regarding function overloading:

assume that you have a function that takes several numeric arguments -
lets for example say 4 arguments: f(a,b,c,d)

Now further assume that internally in the function the first thing you
do is to convert the arguments to double precision, do your calculations
and return the result always in double precision.

In order to allow for all combinations of numeric
(int2,int4,int8,float4,float8) inputs one would now have to define and
write 5^4 = 625 functions !!!!!!!!!!!!!!!

Apart from forcing the function user to use casts, is there any other
way to avoid this madness of writing 625 functions ?

I don't see why you need more than one function, taking all
double-precision arguments ...

regards, tom lane

#3Robert Greimel
greimel@ing.iac.es
In reply to: Tom Lane (#2)
Re: function overloading

On Mon, 2006-01-09 at 20:28, Tom Lane wrote:

Robert Greimel <greimel@ing.iac.es> writes:

I have a question regarding function overloading:

assume that you have a function that takes several numeric arguments -
lets for example say 4 arguments: f(a,b,c,d)

Now further assume that internally in the function the first thing you
do is to convert the arguments to double precision, do your calculations
and return the result always in double precision.

In order to allow for all combinations of numeric
(int2,int4,int8,float4,float8) inputs one would now have to define and
write 5^4 = 625 functions !!!!!!!!!!!!!!!

Apart from forcing the function user to use casts, is there any other
way to avoid this madness of writing 625 functions ?

I don't see why you need more than one function, taking all
double-precision arguments ...

regards, tom lane

You are right.

I got confused by the error message when I first tried to call the
function and had made the mistake to define it with less parameters than
it actually takes. The error for
select f(1,1.5,a,b) from table;
was
ERROR: Function 'f(int4, float8, float4, float4)' does not exist
Unable to identify a function that satisfies the given argument
types
You may need to add explicit typecasts

which made me think that I have to define a function for every possible
combination of numeric types. By the time I realized that I had missed a
parameter I already had added casts to all parameters in the query.

So it works as I would expect it - one function definition with all
arguments as double is sufficient as you note.

Greetings

Robert