Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:117538 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 27704 invoked from network); 17 Apr 2022 03:15:52 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 17 Apr 2022 03:15:52 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 3124C18033A for ; Sat, 16 Apr 2022 21:48:46 -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=-0.0 required=5.0 tests=BAYES_40,DKIM_SIGNED, DKIM_VALID,HTML_MESSAGE,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-qv1-f54.google.com (mail-qv1-f54.google.com [209.85.219.54]) (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 ; Sat, 16 Apr 2022 21:48:45 -0700 (PDT) Received: by mail-qv1-f54.google.com with SMTP id x20so8992311qvl.10 for ; Sat, 16 Apr 2022 21:48:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=newclarity-net.20210112.gappssmtp.com; s=20210112; h=from:mime-version:subject:message-id:date:to; bh=/+yczZ6xjX79rSpES3+yMLCA2IovL5TUcDFZb1j87hQ=; b=sjMupeUUba/Vi/uUt+N8Luqr6oTiY9k2Hhca95n5NJckSFZNno4x4Mhi4xf1fx3Jio b817Lsf8GQhcWloT3HS+eSTt3IXwm/ZUmGNZCgTdQGL94VTDSL+qBG8p6OfkYAZ+p3e/ 40GsruP87Tl/0KMRYReEy+K1Btt/4r24Ox6MG8RO+vTMrnqLzRuE4VsEVNAKCun4vBtm IPi/b0MSUqh2RmOd9S0rn94lqsPA3GjlmnevEL+R0RDSVIovC9AVFWyHLVky6eSW6ryw sZdtGvT8mALpkll2W0BJiMYPmpotHPPOPHahyzXWB+Fst/gtQD1VPAIb9UpVL0tjuQ8r wuEg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:mime-version:subject:message-id:date:to; bh=/+yczZ6xjX79rSpES3+yMLCA2IovL5TUcDFZb1j87hQ=; b=2nLGZNWSZWXOZ/HWRMKA2U9oaJ4vwD18bfiyelHSk85imxCw9sY12yky65g+k4bWZj 6btF2dP+FdZ519DKwEP4EaOAwb08AEXv6hwRq3MZtn+DdNVpDQhJ9S7oI/A18zApnD7l HzApharAwdPuibqI+IbvYbFsKvrlccs48pmxxKOvykN9W4zd5tuX7M2hOj+Ux3YkHqpk U/Xhy8fee99YujDYoaPIX1bGDTO5cE6hdFwRk8IYqTS5d6aE0Lnp5bLW96ckZ0JhE1S5 LGkgrnKz2SWqsOV20sMczlgk+jImL1aTO/Hc+vHUTXGDZlMLz/Dl7HVhGMHp4RSr/Tde 5V4w== X-Gm-Message-State: AOAM5327q64upe5vYONyOCFF7aGKNurHCBiXtfS5D03rVCr+sBhJ+mFT ViDIrK0/xiQs4LgyfpSeoa598VYYPIRldA== X-Google-Smtp-Source: ABdhPJxmunGNzDMkxje1VFjHuwWi+edDeo8KSsaLFAX0onVOV5Cy6KkrZn6RjOfINMfDUX4VgVfu7A== X-Received: by 2002:a0c:8d07:0:b0:443:a6be:7774 with SMTP id r7-20020a0c8d07000000b00443a6be7774mr4103928qvb.51.1650170924563; Sat, 16 Apr 2022 21:48:44 -0700 (PDT) Received: from [192.168.1.10] (c-24-98-254-8.hsd1.ga.comcast.net. [24.98.254.8]) by smtp.gmail.com with ESMTPSA id f11-20020a05620a12eb00b0069c88d15b6asm3769505qkl.68.2022.04.16.21.48.43 for (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sat, 16 Apr 2022 21:48:44 -0700 (PDT) Content-Type: multipart/alternative; boundary="Apple-Mail=_2D889117-67BF-45E6-BD8B-321D7720B21C" Mime-Version: 1.0 (Mac OS X Mail 13.4 \(3608.120.23.2.7\)) Message-ID: <280B4771-81A8-44DE-9709-A4657520595C@newclarity.net> Date: Sun, 17 Apr 2022 00:48:43 -0400 To: PHP internals X-Mailer: Apple Mail (2.3608.120.23.2.7) Subject: Constraints vs. values as types? From: mike@newclarity.net (Mike Schinkel) --Apple-Mail=_2D889117-67BF-45E6-BD8B-321D7720B21C Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=us-ascii Hi PHPers, As I have been following the discussion about the need for a "true" type = to allow documenting that a function should only be able to return true = vs. bool 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 before. 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 because I am mostly doing Go programming now and constraints is = effectively how Go handled an equivalent conundrum for the Generics they = just added. 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 side are expressions (I was just spitballing the syntax; other = syntaxes or even concept names might represent this concept better): constraint true: true constraint false: false If we had constraints in PHP we could also have things like this: constraint Truthy: !empty($this) constraint Falsey: empty($this) constraint Percent: is_numeric($this) && 0 <=3D $this && $this = <=3D100 constraint DateTimeable: is_object($this) && (get_class=3D($this) =3D=3D DateTime::class || = get_class=3D($this) =3D=3D DateTimeImmutable::class) constraint ProductID: is_string($this) && strlen($this)=3D=3D10=20= && preg_match("#[A-Z]{2}[0-9]{8}#",$this) With the above, we could write functions like this and know they are = more "type safe" than otherwise: function product_exists(ProductID $product_id):bool {...} Constraints could also be limited to using only standard library = functions so that it would be possible to do static analysis on a = constraint. Clearly 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 true with a type. =20 FWIW I am just offering up an approach that seems to have worked well in = Go for consideration. If you like it, please discuss it. If not, it was = worth a try. -Mike P.S. I am sure there are many ways to improve what I proposed. Note = this is *just* a straw man proposal; please offer suggestions for = improvement. --Apple-Mail=_2D889117-67BF-45E6-BD8B-321D7720B21C--