On 27/06/2022 13:03, Guilliam Xavier wrote:
Reminded me of e.g.
https://stackoverflow.com/questions/6422380/does-any-programming-language-support-defining-constraints-on-primitive-data-types
(mainly integer ranges, but the concept of "domains" looks similar)
Yes, I'm vaguely familiar with the facilities provided by Pascal (via
Delphi) and SQL (via Postgres).
Looking at the Postgres docs, it has both "enumerated" and "domain"
types, with exactly the distinction that's relevant here.
Enums are type safe:
> Each enumerated data type is separate and cannot be compared with
other enumerated types
https://www.postgresql.org/docs/current/datatype-enum.html
They can be explicitly cast to "text" (i.e. string), but will not be
cast implicitly when passed to a function or operator, so it ends up
equivalent to our case of calling ->value, but without the flexibility
of distinguishing the "case name" and "backing value".
Whereas domains are sub-types of some other type:
> When an operator or function of the underlying type is applied to a
domain value, the domain is automatically down-cast to the underlying type.
https://www.postgresql.org/docs/current/domains.html
This seems to be what people are asking for here: the values are
constrained when the domain is explicitly mentioned, but freely
interchangeable with the underlying type.
Here's an online demo showing the difference:
https://dbfiddle.uk/?rdbms=postgres_14&fiddle=88639144aec58ab7cf7e34a0c103aa51
Regards,
--
Rowan Tommins
[IMSoP]
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: https://www.php.net/unsub.php