Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:113522 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 72341 invoked from network); 14 Mar 2021 22:06:09 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 14 Mar 2021 22:06:09 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 5637C1804B8 for ; Sun, 14 Mar 2021 14:59:28 -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=-1.6 required=5.0 tests=BAYES_00,HTML_MESSAGE, MANY_SPAN_IN_TEXT,RCVD_IN_DNSWL_LOW,SPF_HELO_NONE,SPF_NONE autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from wp160.webpack.hosteurope.de (wp160.webpack.hosteurope.de [80.237.132.167]) (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, 14 Mar 2021 14:59:26 -0700 (PDT) Received: from [2a02:8109:9d40:1d44:40b6:f5d3:c567:d46c] (helo=nas.fritz.box); authenticated by wp160.webpack.hosteurope.de running ExIM with esmtpsa (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) id 1lLYlh-0002pd-9s; Sun, 14 Mar 2021 22:59:25 +0100 To: PHP internals Message-ID: <2ff5377c-3718-feb1-ba20-96df47357414@mabe.berlin> Date: Sun, 14 Mar 2021 22:59:25 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.8.0 MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="------------F03136770B31D38CD0A49652" Content-Language: en-US X-bounce-key: webpack.hosteurope.de;marc@mabe.berlin;1615759168;22dd3f21; X-HE-SMSGID: 1lLYlh-0002pd-9s Subject: Inconsistency in enumerations RFC From: marc@mabe.berlin (Marc) --------------F03136770B31D38CD0A49652 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Hi I'm trying out the new enumeration support for PHP 8.1 using https://github.com/php/php-src/pull/6489 thought to implement a polyfill based on class constants and reflection. Doing so I come across the newly defined interfaces: ``` interface UnitEnum{ public string$name; public staticfunction cases(): array ; } interface BackedEnumextends UnitEnum{ public string$value; public staticfunction from(int|string$scalar): static; public staticfunction tryFrom(int|string$scalar): ?static; } ``` My interest here is on BackedEnum which (currently) supports int|string. First of all the interface defines a "public string $value" but in fact the type needs to be "int|string". As properties are not supported in interfaces this is a pure documentation issue in the RFC itself. Secondly the arguments name $scalar is not really a good name - now as we have named arguments - $value would be much more meaningful. But most interestingly I get the feeling that this should be split into several typed interfaces because this makes wrong impression especially that "tryFrom" would except int|string but actually it doesn't. Additionally, if we want to support other types later on this would blow up the types even more. Example: ``` enum Test:string { case TEST = '1'; } Test::tryFrom('unknown'); // NULL Test::tryFrom(1);// TypeError ``` The TypeError doesn't make sense here as it matches the type and it even matches the string representation of an existing type. I think it would be much clearer if there would be two different interfaces like: ``` interface IntEnum extends UnitEnum { public int $value; public static function from(int$value): static; public static function tryFrom(int$value): ?static; } interface StringEnum extends UnitEnum { public string $value; public static function from(string $value): static; public static function tryFrom(string $value): ?static; } ``` or else the above example should return a NULL or even case matching the string representation instead of TypeError as it matches the defined argument types. Thoughts? Thanks, Marc --------------F03136770B31D38CD0A49652--