Thread: Creating operators

Creating operators

From
"Gene Sokolov"
Date:
Since no one replied in pg-general, I am reposting to hackers.

Q1:
Let's say I want to create a '+' operator for <my own type> + int4. Do I
really have to
define two '+' operators, one

<my own type> + int

and the other

int + <my own type>

Q2:

Can I create an operator '::', such as <my own type>::double ?

Gene Sokolov.





Re: [HACKERS] Creating operators

From
Tom Lane
Date:
"Gene Sokolov" <hook@aktrad.ru> writes:
> Q1:
> Let's say I want to create a '+' operator for <my own type> + int4. Do I
> really have to
> define two '+' operators, one
> <my own type> + int
> and the other
> int + <my own type>

Yes.  There's nothing compelling them to behave the same, after all
(consider '-' instead of '+').  If they do behave the same you
should indicate this with "commutator" links --- see the discussion
in the manual.

> Can I create an operator '::', such as <my own type>::double ?

You can't redefine the meaning of the typecast construct '::',
if that's what you meant.  But perhaps what you really meant
was that you want to provide a conversion from your type to
double.  For that you just make a function named 'double',
yielding double, and taking your type as input.  The typecast
code will use it automatically.

(Of course "double" is spelled "float8" in Postgres-land,
but you knew that...)
        regards, tom lane


Re: [HACKERS] Creating operators

From
Thomas Lockhart
Date:
> > Can I create an operator '::', such as <my own type>::double ?

This brings up something: I see mention in Date and Darwen of an SQL3
enumerated type. Nice feature, but they show the syntax being:
 <type>::<value>

which reuses the "::" operator in a way which may be incompatible with
Postgres' usage (seems to me to have the fields reversed). btw, I
could imagine implementing enumerated types as Postgres arrays or as a
separate table per type.

Has anyone come across this SQL3 feature yet? Any opinions??
                  - Thomas

-- 
Thomas Lockhart                lockhart@alumni.caltech.edu
South Pasadena, California