Home Explore Blog CI



postgresql

5th chunk of `doc/src/sgml/ref/create_cast.sgml`
92d0c93648bc41a85874c285f3a56132a24bceb1078c9b350000000100000983
 types both ways you
   need to declare casts both ways explicitly.
  </para>

 <indexterm zone="sql-createcast">
  <primary>cast</primary>
  <secondary>I/O conversion</secondary>
 </indexterm>

  <para>
   It is normally not necessary to create casts between user-defined types
   and the standard string types (<type>text</type>, <type>varchar</type>, and
   <type>char(<replaceable>n</replaceable>)</type>, as well as user-defined types that
   are defined to be in the string category).  <productname>PostgreSQL</productname>
   provides automatic I/O conversion casts for that. The automatic casts to
   string types are treated as assignment casts, while the automatic casts
   from string types are
   explicit-only.  You can override this behavior by declaring your own
   cast to replace an automatic cast, but usually the only reason to
   do so is if you want the conversion to be more easily invokable than the
   standard assignment-only or explicit-only setting.  Another possible
   reason is that you want the conversion to behave differently from the
   type's I/O function; but that is sufficiently surprising that you
   should think twice about whether it's a good idea.  (A small number of
   the built-in types do indeed have different behaviors for conversions,
   mostly because of requirements of the SQL standard.)
  </para>

  <para>
   While not required, it is recommended that you continue to follow this old
   convention of naming cast implementation functions after the target data
   type.  Many users are used to being able to cast data types using a
   function-style notation, that is
   <replaceable>typename</replaceable>(<replaceable>x</replaceable>).  This notation is in fact
   nothing more nor less than a call of the cast implementation function; it
   is not specially treated as a cast.  If your conversion functions are not
   named to support this convention then you will have surprised users.
   Since <productname>PostgreSQL</productname> allows overloading of the same function
   name with different argument types, there is no difficulty in having
   multiple conversion functions from different types that all use the
   target type's name.
  </para>

  <note>
   <para>
    Actually the preceding paragraph is an oversimplification: there are
    two cases in which a function-call construct will be treated as a cast
    request without having matched it to an actual function.

Title: Automatic I/O Conversion Casts and Naming Conventions for Cast Implementation Functions
Summary
This section discusses automatic I/O conversion casts between user-defined and standard string types in PostgreSQL, noting that manual casts are typically unnecessary. Automatic casts to string types are assignment casts, while those from string types are explicit-only. Overriding these with custom casts is possible, mainly to change invokability. The section recommends naming cast implementation functions after the target data type for user convenience and compatibility with function-style casting notation (typename(x)). PostgreSQL's function overloading allows multiple conversion functions with the same name for different source types.