Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:117539 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 65098 invoked from network); 17 Apr 2022 19:15:04 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 17 Apr 2022 19:15:04 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id BA16118037E for ; Sun, 17 Apr 2022 13:48:05 -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,HTML_MESSAGE,MIME_QP_LONG_LINE, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE, 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: Received: from mail-qk1-f172.google.com (mail-qk1-f172.google.com [209.85.222.172]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Sun, 17 Apr 2022 13:48:05 -0700 (PDT) Received: by mail-qk1-f172.google.com with SMTP id a186so7343708qkc.10 for ; Sun, 17 Apr 2022 13:48:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=benramsey.com; s=google; h=content-transfer-encoding:from:mime-version:subject:date:message-id :references:cc:in-reply-to:to; bh=qGkBMSf87+jFRqUclSF2PUJ6I8gQaAYGP3zUJHc7wko=; b=VxtFb0zSQh63jBWy5X8iSqgsGV7/UtiH4ckFcH6IJzNleSb90YO4r2CdMHUS71jprH 1HuAWlphVrTfG5ThDaVPMdE4g1h/IVoVwGHJBswCRDGo0EWJOzQErO1n4k+An90KjAu7 7ATO3Tp6QeMcxlB1OJmk1/ADnREF+Y86P/CvarT7QqtgLDdedY5UxkiekdFtH7BJYecy 59Ybf6syozmxxnq0veOfKnOa3WbSq4Vu6s7nrsPbIWhvRAhPEJx+zp4uGU3zzmeKxvwR y4WozqHz11iQNiZ8jC2Vd+NmPqywKy9Sl3qDzRgFBfzrh7RRxTnNyF2Nyu23lcKVcJQ2 2bGg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:content-transfer-encoding:from:mime-version :subject:date:message-id:references:cc:in-reply-to:to; bh=qGkBMSf87+jFRqUclSF2PUJ6I8gQaAYGP3zUJHc7wko=; b=SM9ymY7W/QE34nLVkrn2K+RF+kCZayVFAVGioWeaqUUyYveN+LCLDL4/s6AGDjV6RN S4gp0YXsfaikmnCwnrIa427oXmcFcxYsEu9v+itYaoSnkXPZ1g17FNHkEXexeuOleAuA QilVU/MMIYBt9y0SaQeiwt2dg4aS0ne6Ac6GL5JyZNXcf0H0dkLSlgBFSBxRp2aarYi9 ujVQI9TMNb6Qj89qxSXRf9N/fAh9SVtJNXfNGwkC9Bp1HlkKbSi3IlUA/2e4q2Q7OPGF R49ZGfMVqDTHu4lMPFbkfuy/BIN+IwPij9H2Mf8QWHtNhzAdvMPbDhbV7WapOIn03lII 23Eg== X-Gm-Message-State: AOAM5335/NQ1mND8UMue9cTPbZCNsFDdkddvnvGGUKTRvE0CYvUNki9F r4JpiPOR5pFkXV4pfjXIn6BFdw== X-Google-Smtp-Source: ABdhPJz2klosqqqal8mOiPdi0HiCez0gwi4YEi04fYMKS+gwRrAr1HGwy0NrnRE8KHlueu4xqnl1TQ== X-Received: by 2002:a05:620a:20c1:b0:69c:2743:7ab1 with SMTP id f1-20020a05620a20c100b0069c27437ab1mr4728475qka.285.1650228484358; Sun, 17 Apr 2022 13:48:04 -0700 (PDT) Received: from smtpclient.apple ([12.204.223.242]) by smtp.gmail.com with ESMTPSA id v17-20020ac85791000000b002f1ec31ed68sm4792647qta.91.2022.04.17.13.48.03 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 17 Apr 2022 13:48:03 -0700 (PDT) Content-Type: multipart/alternative; boundary=Apple-Mail-57495BED-755F-45C0-A326-A487E2630FFB Content-Transfer-Encoding: 7bit Mime-Version: 1.0 (1.0) Date: Sun, 17 Apr 2022 15:48:02 -0500 Message-ID: <0DB112AE-E7A7-4479-96C5-DCCC28E547A2@benramsey.com> References: <280B4771-81A8-44DE-9709-A4657520595C@newclarity.net> Cc: PHP internals In-Reply-To: <280B4771-81A8-44DE-9709-A4657520595C@newclarity.net> To: Mike Schinkel X-Mailer: iPhone Mail (19E258) Subject: Re: [PHP-DEV] Constraints vs. values as types? From: ben@benramsey.com (Ben Ramsey) --Apple-Mail-57495BED-755F-45C0-A326-A487E2630FFB Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable > On Apr 16, 2022, at 23:48, Mike Schinkel wrote: >=20 > =EF=BB=BFHi PHPers, >=20 > As I have been following the discussion about the need for a "true" type t= o allow documenting that a function should only be able to return true vs. b= ool which would allow false, and in the past the same argument for false vs.= true, it occurs to me that there is another approach to address this rather= than add values to be types which AFAIK has not been mentioned on the list b= efore. >=20 > An alternate solution to possibly consider might be to add the concept of "= constraints" to PHP that could work alongside types. This occured to me beca= use I am mostly doing Go programming now and constraints is effectively how G= o handled an equivalent conundrum for the Generics they just added. >=20 > Consider if PHP allowed the following and if PHP would allow type hints to= use *either* a type *or* a constraint where the values on the right hand si= de are expressions (I was just spitballing the syntax; other syntaxes or eve= n concept names might represent this concept better): >=20 > constraint true: true >=20 > constraint false: false >=20 > If we had constraints in PHP we could also have things like this: >=20 > constraint Truthy: !empty($this) >=20 > constraint Falsey: empty($this) >=20 > constraint Percent: is_numeric($this) && 0 <=3D $this && $this <=3D100 >=20 > constraint DateTimeable: is_object($this) > && (get_class=3D($this) =3D=3D DateTime::class || get_class=3D($thi= s) =3D=3D DateTimeImmutable::class) >=20 > constraint ProductID: is_string($this) && strlen($this)=3D=3D10=20 > && preg_match("#[A-Z]{2}[0-9]{8}#",$this) >=20 > With the above, we could write functions like this and know they are more "= type safe" than otherwise: >=20 > function product_exists(ProductID $product_id):bool {...} >=20 > Constraints could also be limited to using only standard library functions= so that it would be possible to do static analysis on a constraint. Clearl= y a static analyzer could evaluate the constraints I represented above. And I= *think* this could be backward compatible with false as a type, and even tr= ue with a type. =20 >=20 > FWIW I am just offering up an approach that seems to have worked well in G= o for consideration. If you like it, please discuss it. If not, it was worth= a try. >=20 > -Mike >=20 > P.S. I am sure there are many ways to improve what I proposed. Note this i= s *just* a straw man proposal; please offer suggestions for improvement. >=20 This reminds me of some of the discussion we=E2=80=99ve had on list of type a= liasing.[^1] I really like this idea and think it could be combined with type aliasing to= create powerful userland-defined types. While you mention restricting to standard-library functions for easier type-= safety, I=E2=80=99m not sure it has to have that limitation. What we=E2=80=99= re effectively defining here is a kind of short-hand Closure that is then ca= lled with the passed value at runtime to validate the type. A type alias cou= ld work similarly, perhaps even with the same syntax. Cheers, Ben [^1]: https://externals.io/message/112209#112214= --Apple-Mail-57495BED-755F-45C0-A326-A487E2630FFB--