Re: [RFC] [Under Discussion] Auto-implement Stringable for string backed enums
| From: | Rowan Tommins | Date: | Mon, 27 Jun 2022 18:01:49 +0000 |
| Subject: | Re: [RFC] [Under Discussion] Auto-implement Stringable for string backed enums | ||
| References: | 1 2 3 4 5 6 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-118110@lists.php.net to get a copy of this message | ||
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 typeshttps://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]