Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:119760 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 26765 invoked from network); 29 Mar 2023 09:05:00 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 29 Mar 2023 09:05:00 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id DA0A71804F8 for ; Wed, 29 Mar 2023 02:04:59 -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-vs1-f53.google.com (mail-vs1-f53.google.com [209.85.217.53]) (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:04:59 -0700 (PDT) Received: by mail-vs1-f53.google.com with SMTP id c1so12740366vsk.2 for ; Wed, 29 Mar 2023 02:04:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1680080699; x=1682672699; 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=NVEkH0DCqYGcBcW8bphtUezr+eD5OxHTQFGGu7zDO28=; b=qTXk3PrQL6Wu2kcSCZ537RbTxLBufWaSqnBN0iWBGPG8taQRGBgWN6TjSYSz4qduaI N6bkmUYWC792DCiHMPjExNTI2qdLxy8i6tVIspURzmmbk4puK/Z5qV5xflzMcITj9IOB /d/C/XFL/u4XZ+IvHkhjJq8uAB/DfP/surOrSqPTGPqmQq7G6V7ULpSIjTe9/YJe3t6h INfKex2kfpu2QATB6dGRN/DRvShFnX79FmmJUG5aIa4yiyJfWNyX/nOIgQkUX9/UHE0F tmIS88gsEhS1c5Mp6x5TFw7p8eel1Uz5B77O7wGo2j2aua354PN0MAT+jTry3Wq+8qR+ 4xLQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680080699; x=1682672699; 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=NVEkH0DCqYGcBcW8bphtUezr+eD5OxHTQFGGu7zDO28=; b=P17M/43rEYlGedlWAD7MxOpOEoymIjXKtIdaRCHo+z1gQE1u92+xrWlRN/+dPDTDzO 2QTqczgRW1XSUOUIsSrUP3uYjrFcPOKmzq4dnBlu0sETXrzChOnzhiJZyLIvOZAjXnXd 7WPU02dpeLi3izZy2ywGf+1jP0z78bel0yh5/FkNVm/TKA0jMd1qmdeTdW6qSQhMmZA9 vmG4FiRHFn7aKvEfWAsqqj0mZJ3TRGig91+6h0NsePPF3WHR9RSk72K7oHPVWWKR2nwG 2WV9AkFcXQROvRLhEOVi3Pa0y6tmJFi5fDvMnkasIAjsHf+NDV3LNJasA42fcFUy41O6 OY4Q== X-Gm-Message-State: AAQBX9eK8nqIlMEQ6dMqXNt7UF886TydtmTbJOM9yKbCSez6Nppez+tS A1IDY25X0v1Ee5IOSehc3LEUh6/6S8fGiNMHgE5h4fWjiJg= X-Google-Smtp-Source: AKy350azWltj6IonbaeUUB0eSEWMn98x5g+HjdBRwUkEn4BB/2jBH88E/x0jx76m8POY/yfcbhlK1QaR5I//Oo+E3To= X-Received: by 2002:a05:6102:440b:b0:421:c4a3:b607 with SMTP id df11-20020a056102440b00b00421c4a3b607mr1272644vsb.3.1680080698629; Wed, 29 Mar 2023 02:04:58 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Date: Wed, 29 Mar 2023 12:04:41 +0300 Message-ID: To: Robert Landers Cc: 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?=) > 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 i= n 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 wro= te: > > 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 exte= nd > > 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 use= r > > 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.