Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:119761 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 29351 invoked from network); 29 Mar 2023 09:22:55 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 29 Mar 2023 09:22:55 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 2D1011804DF for ; Wed, 29 Mar 2023 02:22:54 -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_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: Received: from mail-ed1-f43.google.com (mail-ed1-f43.google.com [209.85.208.43]) (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 ; Wed, 29 Mar 2023 02:22:53 -0700 (PDT) Received: by mail-ed1-f43.google.com with SMTP id eh3so60457654edb.11 for ; Wed, 29 Mar 2023 02:22:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1680081772; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=34ovmt+OlD54HPujIyVVVRLbrT2s3PJixEQGZ4ydS6Q=; b=NMp0Q9yRhDwpdev4eolxxvnsoIfpBSJpVSm8iex0MKK+U5Nb2B7fbwC2KUdUpjmaPR qEs7tvJd4d+2Fk0YpYJKQaqR9X2g4vSOaWt7t5qY66uSu09NxXfpNy23j2qN7vWHkQkc EJiZpYFp9V34yyZ80qGtc0Gw5AH1HpaR7R5C/j5AYVraIubNIHrYywIHK0m473aqJsaJ 1nyn/vaJs999j0kOOKynaIPW3AUtJjLaumo2c0avV7Cxh9ASlIX6WZBLxlWl0maFKuzB pZpXwU5IP9SpaQDawDIomf2k6DbWm5jJlm+wcSDzZLbdTJGYtXNE5JUNpYlYzkBRTdQr wvoQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680081772; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=34ovmt+OlD54HPujIyVVVRLbrT2s3PJixEQGZ4ydS6Q=; b=NSNB1BtDQOF4PvcO629d3Yl64h1blmuzUdaQF49gYzZGpUybDdMCShpQPZvZ4OTvz4 Vb9dYl5n7wwZyQ4AQ/192XGFALiLsuXb/5J/4RGk2oDso8cBhqq8Wu1n4IcbyzoVsTKo Lc67cNTEZ9OeMCq7R3ZsvscUe6+JqSnbjVR9JHj+J3r9At+oXNGvpOanwGJz8SQ24m7c VwD3vUp2WpAFIVYZyxVAUA54qpdKQHsCR7+3zD4gNNznwdedot4wzrDms4/ETzP3wpVz 2ti5tAQ46dWpyatMo1T+/5zCcYB1+ve0H2VEKgyfgWCyVkGc42yPBtJQblE8sywDIA9y SzYQ== X-Gm-Message-State: AAQBX9fuh3B1G323IIo4o5YwpSBRWBZymMEsQkbr1XSC/1bIwK6H0IpJ KTDXuOPYCewiyHdQUq0LZzyn/S8OjAtfNCnTB3D2VW5Q8HY= X-Google-Smtp-Source: AKy350ZwaITx+f2cxPFVhfibk9Pnyy4xrbMAES+I5Ckt0peTorWzP/1+vSiQSUWG0Oi6U5KgdR53oKXjV0zMpjVx9L0= X-Received: by 2002:a17:906:6a93:b0:92d:591f:645f with SMTP id p19-20020a1709066a9300b0092d591f645fmr9970182ejr.5.1680081772295; Wed, 29 Mar 2023 02:22:52 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Date: Wed, 29 Mar 2023 11:22:41 +0200 Message-ID: To: =?UTF-8?Q?Rokas_=C5=A0leinius?= Cc: Robert Landers , internals@lists.php.net Content-Type: multipart/alternative; boundary="0000000000003b380b05f80685e9" Subject: Re: [PHP-DEV] [IDEA] allow extending enum From: ocramius@gmail.com (Marco Pivetta) --0000000000003b380b05f80685e9 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Hey Rokas, The idea of declaring an enum `abstract` is potentially worth exploring (I'm not convinced, but go for it), but beware that removing the implicit `final` from ENUMs is a BC break, since it breaks the assumption that an ENUM is a sealed type. If you design a proposal now, you need to consider that `final` as the implicit default is an existing contract that really enables ENUM usage. Marco Pivetta https://mastodon.social/@ocramius https://ocramius.github.io/ On Wed, 29 Mar 2023 at 11:05, Rokas =C5=A0leinius wrote= : > > to restrict options within a certain range. > Okay that's an aspect of enums that I never gave much thought to, but > you're completely right. > > However to be explicit about that aspect of functionality one would > then have to also be > allowed to define final enums. > > That way you still have both options - to restrict the option set, but > to also allow adding > custom ones while benefiting from other enum advantages. > > Thus, rfc idea seems to be: > * enums should be able to extend other enums; > * abstract enums; > * enums allowed to implement interfaces; > * final enums (NEW!) > > > > > BackedEnum&MyCustomBehaviorInt > That's a fascinating approach but to a different problem than illustrated > in OP. > > Would you mind sharing (in private preferably to not distract from the > discussion) the > gist of the problem this solves? I'm really curious, as I've yet to > use intersect types :)! > > On Wed, 29 Mar 2023 at 11:47, Robert Landers > wrote: > > > > On Wed, Mar 29, 2023 at 10:31=E2=80=AFAM Rokas =C5=A0leinius > wrote: > > > > > > Enums were a very useful addition to PHP, however one aspect of them > is neither > > > explicitly documented - or seemingly even talked about. > > > > > > Enums were implemented as final so they cannot be extended nor can > extend > > > anything else. > > > > > > From a user perspective it's surprising - and actually limiting. > > > > > > USAGE EXAMPLE: > > > I am making an error management system: each error presented to the > user > > > must have a unique code visible. > > > > > > ```php > > > class SystemError > > > { > > > public function __construct( > > > private string $errorText, > > > private ErrorCode $code > > > ) { > > > } > > > > > > public function __toString(): > > > { > > > return $this->errorText . ' ' . $this->code->toString(); > > > } > > > } > > > // ... > > > > > > enum ErrorCode > > > { > > > case Code_1; > > > case Code_2; > > > > > > public function toString(): string > > > { > > > return 'Error code:' . substr($this->name, strlen('Code_')); > > > } > > > } > > > ``` > > > > > > Now I want to modify it to support different modules with different > > > namespaces for > > > errors, e.g. an ApiError, simple enough: > > > > > > ```php > > > enum BaseErrorCode > > > { > > > // ... > > > } > > > > > > enum ErrorCode extends BaseErrorCode > > > { > > > case Code_1; > > > case Code_2; > > > > > > // ... > > > } > > > > > > enum ApiErrorCode extends BaseErrorCode { > > > // ... > > > function toString(): string > > > { > > > return 'Error code:API-' . substr($this->name, > strlen('Code_')); > > > } > > > } > > > ``` > > > > > > This results in a syntax error. > > > > > > PROPOSAL: > > > > > > Enums should be able to extend other enums. > > > > > > For a complete wishlist, add: > > > * abstract enums; > > > * enums allowed to implement interfaces; > > > > > > However since I have no experience in PHP source code, I can only > > > provide the test suite for a possible PR this might have :( > > > > > > Do you think this is likely to get implemented? > > > > > > -- > > > PHP Internals - PHP Runtime Development Mailing List > > > To unsubscribe, visit: https://www.php.net/unsub.php > > > > > > > Hey Rokas, > > > > My approach has been to use an intersection type like: > > BackedEnum&MyCustomBehaviorInterface > > > > This works for me but it'd be interesting if something like that > > wouldn't work for you. > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: https://www.php.net/unsub.php > > --0000000000003b380b05f80685e9--