Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:117414 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 10915 invoked from network); 24 Mar 2022 13:39:58 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 24 Mar 2022 13:39:58 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id AC54C1804A9 for ; Thu, 24 Mar 2022 08:06:58 -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,HTML_MESSAGE, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,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: Received: from mail-ej1-f53.google.com (mail-ej1-f53.google.com [209.85.218.53]) (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 ; Thu, 24 Mar 2022 08:06:58 -0700 (PDT) Received: by mail-ej1-f53.google.com with SMTP id pv16so9835272ejb.0 for ; Thu, 24 Mar 2022 08:06:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=MZmVLss8ZJp+dnRtgEpMiv4Sk67x+4w9L4LAe+Q3cjM=; b=E5aGy1yRp9qKRdT/FVLU496KP0u3NCqpvxyJDwVZOFguyOvR5SArWVtD/pZUB6R/7w xc9qgKh14Kbpv2UsL+Ul0/sDkiz7y51nVahQgUr+RTiZzgUAG6bzsLnSG/iZliQR2Dr3 m506qaiN33BekBZzgs6kBM1SjB/hVz+/WosEf8wn0ecEKx85bJt0arF5KNP2guojTgZW o+hoA7DilQAkQrSMNHhN6liBWG/M0ZiXLxPnrqLotZDrwOhv97gmeyIL8Fdkq0xD3LB6 Hsx3lMInhclaEVzc6AcAvcG8F4nW8UQuQEW5rK9B7glH/T78r1ykKE6wsxuEKPwPG1uz kbSw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=MZmVLss8ZJp+dnRtgEpMiv4Sk67x+4w9L4LAe+Q3cjM=; b=dbHnB5GleGCj1QrOVg7lN/ywh63HVh1TOaKeWHYZLfiyNss1r72/wx8XQlABHUmRmj XlVS3p/ZWasUCOs4QamrwDOOH5P65q7rVXFL3CrDGwDO7P3M7hQ+zRGgZTTCS1zQTErL svXe4frQTIJAOw5B99n5HBGDczsGwXrHE+Af2c8eXD+zLeXAgxXQIHJP0/j6clnndKty eSaT7B5oSJoN7Wn6ORW+vjFsccq5cY1Dn8K8pCj+XWpJGg+/0j4djqKFLsG4lzONQRz/ pPcxDbcTbjdoqFd70tpnFMgAAObStS8FaT/UqASyBRV29q4IR3Q4NjEbgx6/FqCBxRYX /lLw== X-Gm-Message-State: AOAM531nGbScbrR/SM1r0cv5IVmv+2YqNkxNm2E/zbCIkBjIfEytioAJ CVm9wTMAJ5RSL4gj9ygqY/i3pw1N+8EUGTzpPPXiq6B+ X-Google-Smtp-Source: ABdhPJxHb6M8N2rVB5szhfM8bZ3uTML/zgjnNWNC5j1pLlTvK8Nl4ajgxqRYr6GW30cTfXVwNEBmEY81Kj7uOBVkaeM= X-Received: by 2002:a17:907:ea1:b0:6df:7900:e4de with SMTP id ho33-20020a1709070ea100b006df7900e4demr6162572ejc.26.1648134416777; Thu, 24 Mar 2022 08:06:56 -0700 (PDT) MIME-Version: 1.0 References: <76c399cb-fb29-4583-a212-8eb69740c96b@www.fastmail.com> <95fb2485-fa60-a107-5fd2-acdcef09bca3@processus.org> In-Reply-To: <95fb2485-fa60-a107-5fd2-acdcef09bca3@processus.org> Date: Thu, 24 Mar 2022 09:06:45 -0600 Message-ID: To: Pierre Cc: Larry Garfield , php internals Content-Type: multipart/alternative; boundary="0000000000007444f205daf8321d" Subject: Re: [PHP-DEV] Typed constants revisited From: mbniebergall@gmail.com (Mark Niebergall) --0000000000007444f205daf8321d Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Pierre, On Thu, Mar 24, 2022 at 1:04 AM Pierre wrote: > Le 23/03/2022 =C3=A0 23:10, Larry Garfield a =C3=A9crit : > > Is there a benefit to it other than "well everything else has types now= , > so..."? Even if it's esoteric, like in reflection-based meta programming= ? > (I've been dabbling too much in that lately. :-) ) "Everything else does= " > isn't a compelling argument for me, but there may be others I'm not > thinking of. > > > > --Larry Garfield > > > Hello, > > Well I was thinking myself that const being well... constant, and so > always defined, type could be determined at compile time without the > need to express it in a verbose way. > > PHP doesn't do any static type inference, but in this case, it could be > done almost for free, am I wrong ? If it was determined at compile time, > then the type checking on overrides would only be trivial, using the > parent occurrence value inferred type. My point is that there's no need > to express the type of a value, the value has already has a type. > As a developer, I don't *always* trust other developers implementing my code. Referring back to the Bird and EmperorPenguin example, the expected type for `public const bool CAN_FLY;` example MUST be a bool. If it isn't typed, another developer _could_ incorrectly set `CAN_FLY =3D 1;` or `CAN_F= LY =3D 'true';` or even `CAN_FLY =3D 'Y';`. With class constants typed, they w= ould get an error if setting it to anything other than a bool `CAN_FLY =3D true;= ` or `CAN_FLY =3D false;`. This further proliferates into sending the value o= f `CAN_FLY` into a typed argument like `protected function determineModeOfTransport(bool $canFly)` - if the constant is set incorrectly as a string or integer then more errors can occur. Typing class constants could also help static code analyzers more easily determine if there are bugs with unexpected value types being used in code. Instead of determining the value of the constant, the type of the constant could be used instead, providing a cleaner tokenized parsing. So you are correct, the const value does have a value that has a type, but there is no way to enforce which type the value is or to use const with inheritance, which is part of the bigger picture here. > > Regards, > > -- > > Pierre > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: https://www.php.net/unsub.php > > --0000000000007444f205daf8321d--