Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:119762 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 30973 invoked from network); 29 Mar 2023 09:31:33 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 29 Mar 2023 09:31:33 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 56E60180543 for ; Wed, 29 Mar 2023 02:31:32 -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, 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-ua1-f46.google.com (mail-ua1-f46.google.com [209.85.222.46]) (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:31:31 -0700 (PDT) Received: by mail-ua1-f46.google.com with SMTP id v48so10885971uad.6 for ; Wed, 29 Mar 2023 02:31:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1680082291; x=1682674291; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=5w3lMUPjzbk8HwlYyKUttlr8f+QDxrQeyYuDTPlnUPw=; b=pv21m2FTgiJqHON2kD48CnF6+ph3xassWzRnN+lLiG5by41V/uZwSFGpypvKCke/dW SUFqR+8wmX77KY2jDfMfzo9oOGBAWPnGLYW+jThfMIYjnncfn2SnLJ81q1BwmDCA0/X7 soPfiBTyt9pktuTT+wxn3Pl4xsMJw/l5rwe1J7itJ+DzeedQw5y/vR3HfXXPvLOjAV6s s21pu22R5Tlvn2dO0E3MYeBmWVCwi3dARSFIvWDOcgEryA6R0VkwfAseURKTUZj2XJJG +y4p4C7OhA4et7/8pnQQLbPotSmHXsEmIsTG8UX2CmtweIA7AzDe0tYmL+IbGvyhV8yF pgVg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680082291; x=1682674291; h=content-transfer-encoding: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=5w3lMUPjzbk8HwlYyKUttlr8f+QDxrQeyYuDTPlnUPw=; b=xPgV8NAuEsqBt16kIfj8AyYq0Pr/N9N7i8fxujYDPljyD+YhUPTEMFPKNdReiwfNd/ JxR/UseI3yZ/ay9HjDhWD8Fc828grAcX9DhPkUHYM0fDpyZkAvsDXzkgqdfrr/PnQqm7 fU3YWc4tCWsonrCVWEgDG06BxEqfMssOWtHxqpqfTrtGwrUkBXL0QCo+EReh49g/Dy5k yxzEXpIVbmRukBDER75xYFv/5VYpUFoHtyVA4vvcMdbR2BC9mX4dlFOjBABGpY/iANJp /yvud21H0ok8ztLleFaq28qHQIU6SstuEduJLsvJkWXFT1HEn4jB2J8fa+47q5KQ+zsu wffg== X-Gm-Message-State: AAQBX9d0fGo/sW58gkiTg9isCLukuBpe7aDJHlcOo5tPYWPs7p3+0cIQ aeDSk+trLrIdl1JIo4J0VF89aOZGO5WnNzmm8cs= X-Google-Smtp-Source: AKy350YYuLaq6R/JRTU+qV+BPEA0FHkk3IFvu7EhyzWkfvV2sG0eZu4hMeM9BMtSyh3VA+fQ4mBiXDp63Bp9xeGtxFY= X-Received: by 2002:a1f:2150:0:b0:43b:ce3f:12bd with SMTP id h77-20020a1f2150000000b0043bce3f12bdmr5866513vkh.3.1680082291008; Wed, 29 Mar 2023 02:31:31 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Date: Wed, 29 Mar 2023 12:31:14 +0300 Message-ID: To: Marco Pivetta Cc: Robert Landers , internals@lists.php.net Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Subject: Re: [PHP-DEV] [IDEA] allow extending enum From: raveren@gmail.com (=?UTF-8?Q?Rokas_=C5=A0leinius?=) > BC break Hm, do you really think so? I wouldn't say removing the final attribute from enums actually "breaks" any functionality. It WOULD allow some potentially illicit hacking to the end users of our code, that's true. I'd say a minor possibility of impact. And anyway, isn't that what got you to fall in love with PHP in the first p= lace? Some good ol' illicit hacking of some dude's library :) On Wed, 29 Mar 2023 at 12:22, Marco Pivetta wrote: > > 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 `f= inal` 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 im= plicit 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 wro= te: >> >> > 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 illustrate= d 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 e= xtend >> > > 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 >>