| The PostgreSQL 9.0 Reference Manual - Volume 2 - Programming Guide
by The PostgreSQL Global Development Group Paperback (6"x9"), 478 pages ISBN 9781906966065 RRP £14.95 ($19.95) Sales of this book support the PostgreSQL project! Get a printed copy>>> |
5.2.5 Polymorphic Types
Four pseudo-types of special interest are anyelement,
anyarray, anynonarray, and anyenum,
which are collectively called polymorphic types.
Any function declared using these types is said to be
a polymorphic function. A polymorphic function can
operate on many different data types, with the specific data type(s)
being determined by the data types actually passed to it in a particular
call.
Polymorphic arguments and results are tied to each other and are resolved
to a specific data type when a query calling a polymorphic function is
parsed. Each position (either argument or return value) declared as
anyelement is allowed to have any specific actual
data type, but in any given call they must all be the
same actual type. Each
position declared as anyarray can have any array data type,
but similarly they must all be the same type. If there are
positions declared anyarray and others declared
anyelement, the actual array type in the
anyarray positions must be an array whose elements are
the same type appearing in the anyelement positions.
anynonarray is treated exactly the same as anyelement,
but adds the additional constraint that the actual type must not be
an array type.
anyenum is treated exactly the same as anyelement,
but adds the additional constraint that the actual type must
be an enum type.
Thus, when more than one argument position is declared with a polymorphic
type, the net effect is that only certain combinations of actual argument
types are allowed. For example, a function declared as
equal(anyelement, anyelement) will take any two input values,
so long as they are of the same data type.
When the return value of a function is declared as a polymorphic type,
there must be at least one argument position that is also polymorphic,
and the actual data type supplied as the argument determines the actual
result type for that call. For example, if there were not already
an array subscripting mechanism, one could define a function that
implements subscripting as subscript(anyarray, integer)
returns anyelement. This declaration constrains the actual first
argument to be an array type, and allows the parser to infer the correct
result type from the actual first argument's type. Another example
is that a function declared as f(anyarray) returns anyenum
will only accept arrays of enum types.
Note that anynonarray and anyenum do not represent
separate type variables; they are the same type as
anyelement, just with an additional constraint. For
example, declaring a function as f(anyelement, anyenum)
is equivalent to declaring it as f(anyenum, anyenum):
both actual arguments have to be the same enum type.
A variadic function (one taking a variable number of arguments, as in
section 5.4.5 SQL Functions with Variable Numbers of Arguments) can be
polymorphic: this is accomplished by declaring its last parameter as
VARIADIC anyarray. For purposes of argument
matching and determining the actual result type, such a function behaves
the same as if you had written the appropriate number of
anynonarray parameters.
| ISBN 9781906966065 | The PostgreSQL 9.0 Reference Manual - Volume 2 - Programming Guide | See the print edition |