Newsgroups: php.internals
Path: news.php.net
Xref: news.php.net php.internals:118110
Return-Path: <rowan.collins@gmail.com>
Delivered-To: mailing list internals@lists.php.net
Received: (qmail 16218 invoked from network); 27 Jun 2022 16:11:05 -0000
Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5)
  by pb1.pair.com with SMTP; 27 Jun 2022 16:11:05 -0000
Received: from php-smtp4.php.net (localhost [127.0.0.1])
	by php-smtp4.php.net (Postfix) with ESMTP id D8444180549
	for <internals@lists.php.net>; Mon, 27 Jun 2022 11:01:52 -0700 (PDT)
X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net
X-Spam-Level: 
X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED,
	DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,NICE_REPLY_A,
	RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS,
	T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.2
X-Spam-ASN: AS15169 209.85.128.0/17
X-Spam-Virus: No
X-Envelope-From: <rowan.collins@gmail.com>
Received: from mail-wr1-f48.google.com (mail-wr1-f48.google.com [209.85.221.48])
	(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
	 key-exchange ECDHE (P-256) server-signature RSA-PSS (2048 bits) server-digest SHA256)
	(No client certificate requested)
	by php-smtp4.php.net (Postfix) with ESMTPS
	for <internals@lists.php.net>; Mon, 27 Jun 2022 11:01:52 -0700 (PDT)
Received: by mail-wr1-f48.google.com with SMTP id w17so14162853wrg.7
        for <internals@lists.php.net>; Mon, 27 Jun 2022 11:01:52 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=gmail.com; s=20210112;
        h=message-id:date:mime-version:user-agent:subject:content-language:to
         :references:from:in-reply-to:content-transfer-encoding;
        bh=hly8l+XgO083d7n4HMj6mQ+cpLmOdTNE7L9PLioA6qU=;
        b=PNzdYPSAGdqMVxk0SlVr6mGN7ZLIaWAgXudr5AAH7NPqT/WtNbRtRNSqml1svsvkt4
         SFDjmTthB1t8uKVH8OmkL7vHr2b6/o7b5q/EMMBVv0OEg592T4QxvtICQ6aF0DSmJhmq
         rKq5git0ig4GvEESmb1BqQSgoYsVi1fo2GvECFMrM0e8OnanAsWXlHNbhTkD/r+NkuvY
         EB8dutPOl+49jfQDhsCW1QEEZBCpKTpe1RZEMMwZMLZmtja3KJC5XWZ+yVU1BlQTCqWa
         YT/+ReIRg013kIgp6VqZaogErD8gswYZg+D/so+bSyNlINR/xDl1oscx2ZSur9zUZAvd
         Lq0A==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20210112;
        h=x-gm-message-state:message-id:date:mime-version:user-agent:subject
         :content-language:to:references:from:in-reply-to
         :content-transfer-encoding;
        bh=hly8l+XgO083d7n4HMj6mQ+cpLmOdTNE7L9PLioA6qU=;
        b=piggafQLxIAvvjsy4LsPapPKEzwD0LtWQB0i4dC6Gjgsc/mDnLPOwuuimrnJicJW1w
         UEdVHeKXTTJrd4O1g5tAartWa5tqXDBuURPYM4Dwcq+mmI419CnhWaMCEAMzJCzwVhBn
         RZQoVmmn0sz2fK2eSJMdVU41p1mXXvHDT4pJQU+GiCCWlcgl4jM2LWZNEeX6+lJykqRG
         OyuHeC3bD0mek8S3vHleoRJZVAFpfhKuR2VnFVg8gNMaXW5FW7I+TW0iglVA+zErHOnV
         zv9kAR7zkMqvSsM5OFKZMNR28uK2dfeUDeo7M1rG7MCDfSIGBATwJ7rLKrmbEZs99eSX
         TE7Q==
X-Gm-Message-State: AJIora81sgCW6c/KapmYv1QRj36i8tadtq9lhF0W9lpzlyMIYe/dfOjy
	3DFv76Z8DBnJoTrA/2/SJ2o//2++sBc=
X-Google-Smtp-Source: AGRyM1vW+dK9x9nnAgfbxy3SayWyA2K+1p/2GrCg4rvc7jtlTNBp2+7sskRWtmSdT5LAG+Kq1uKt2w==
X-Received: by 2002:a05:6000:795:b0:21b:b9a0:6010 with SMTP id bu21-20020a056000079500b0021bb9a06010mr13029118wrb.508.1656352911226;
        Mon, 27 Jun 2022 11:01:51 -0700 (PDT)
Received: from [192.168.0.22] (cpc104104-brig22-2-0-cust548.3-3.cable.virginm.net. [82.10.58.37])
        by smtp.googlemail.com with ESMTPSA id h3-20020adfe983000000b0021b97ffa2a9sm11456945wrm.46.2022.06.27.11.01.49
        for <internals@lists.php.net>
        (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128);
        Mon, 27 Jun 2022 11:01:50 -0700 (PDT)
Message-ID: <787cf747-be62-2ebb-7bf6-18cccd70db8c@gmail.com>
Date: Mon, 27 Jun 2022 19:01:49 +0100
MIME-Version: 1.0
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
 Thunderbird/91.10.0
Content-Language: en-GB
To: PHP Internals List <internals@lists.php.net>
References: <CAOWwgpmG33Ybp6KS4ePrMgMJdW5xX+M2mos2cpgoVNUy8dVMjQ@mail.gmail.com>
 <9C11261B-B9D0-4342-81EB-60276B3036E5@php.net>
 <CAOWwgpkd3OEu1VReP8gMnFdwPAQyPywQ9Ttie+grcNeXzsKS2Q@mail.gmail.com>
 <CAC5V6ggkEhf9KExS6TrK_54gzppELBoAceZS3Ouj5C4q6Noj0A@mail.gmail.com>
 <CAOWwgpm71KDp-eBNYbSzL25aAr6dk0992MojN68eNoFK5zjTdQ@mail.gmail.com>
 <CAC5V6ggPfhu6vt+jpzBeUrHk9XGdetZSVqYd0Q=hvYO3EfpvCA@mail.gmail.com>
In-Reply-To: <CAC5V6ggPfhu6vt+jpzBeUrHk9XGdetZSVqYd0Q=hvYO3EfpvCA@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Subject: Re: [PHP-DEV] [RFC] [Under Discussion] Auto-implement Stringable for
 string backed enums
From: rowan.collins@gmail.com (Rowan Tommins)

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]