1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
|
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>32.5.Function Overloading</title>
<link rel="stylesheet" href="stylesheet.css" type="text/css">
<link rev="made" href="pgsql-docs@postgresql.org">
<meta name="generator" content="DocBook XSL Stylesheets V1.70.0">
<link rel="start" href="index.html" title="PostgreSQL 8.1.4 Documentation">
<link rel="up" href="extend.html" title="Chapter32.Extending SQL">
<link rel="prev" href="xfunc-sql.html" title="32.4.Query Language (SQL) Functions">
<link rel="next" href="xfunc-volatility.html" title="32.6.Function Volatility Categories">
<link rel="copyright" href="ln-legalnotice.html" title="Legal Notice">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="sect1" lang="en">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="xfunc-overload"></a>32.5.Function Overloading</h2></div></div></div>
<a name="id705364"></a><p> More than one function may be defined with the same SQL name, so long
as the arguments they take are different. In other words,
function names can be <em class="firstterm">overloaded</em>. When a
query is executed, the server will determine which function to
call from the data types and the number of the provided arguments.
Overloading can also be used to simulate functions with a variable
number of arguments, up to a finite maximum number.
</p>
<p> When creating a family of overloaded functions, one should be
careful not to create ambiguities. For instance, given the
functions
</p>
<pre class="programlisting">CREATE FUNCTION test(int, real) RETURNS ...
CREATE FUNCTION test(smallint, double precision) RETURNS ...</pre>
<p>
it is not immediately clear which function would be called with
some trivial input like <code class="literal">test(1, 1.5)</code>. The
currently implemented resolution rules are described in
<a href="typeconv.html" title="Chapter10.Type Conversion">Chapter10, <i>Type Conversion</i></a>, but it is unwise to design a system that subtly
relies on this behavior.
</p>
<p> A function that takes a single argument of a composite type should
generally not have the same name as any attribute (field) of that type.
Recall that <code class="literal">attribute(table)</code> is considered equivalent
to <code class="literal">table.attribute</code>. In the case that there is an
ambiguity between a function on a composite type and an attribute of
the composite type, the attribute will always be used. It is possible
to override that choice by schema-qualifying the function name
(that is, <code class="literal">schema.func(table)</code>) but it's better to
avoid the problem by not choosing conflicting names.
</p>
<p> When overloading C-language functions, there is an additional
constraint: The C name of each function in the family of
overloaded functions must be different from the C names of all
other functions, either internal or dynamically loaded. If this
rule is violated, the behavior is not portable. You might get a
run-time linker error, or one of the functions will get called
(usually the internal one). The alternative form of the
<code class="literal">AS</code> clause for the SQL <code class="command">CREATE
FUNCTION</code> command decouples the SQL function name from
the function name in the C source code. For instance,
</p>
<pre class="programlisting">CREATE FUNCTION test(int) RETURNS int
AS '<em class="replaceable"><code>filename</code></em>', 'test_1arg'
LANGUAGE C;
CREATE FUNCTION test(int, int) RETURNS int
AS '<em class="replaceable"><code>filename</code></em>', 'test_2arg'
LANGUAGE C;</pre>
<p>
The names of the C functions here reflect one of many possible conventions.
</p>
</div></body>
</html>
|