Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:119772 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 57501 invoked from network); 29 Mar 2023 14:26:13 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 29 Mar 2023 14:26:13 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 7C7BD1804A9 for ; Wed, 29 Mar 2023 07:26:11 -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-f42.google.com (mail-ua1-f42.google.com [209.85.222.42]) (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 07:26:10 -0700 (PDT) Received: by mail-ua1-f42.google.com with SMTP id m5so11464012uae.11 for ; Wed, 29 Mar 2023 07:26:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1680099970; x=1682691970; 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=yI53fJe3tfM7C7UI3uCUCQiatoboVbtQcUEC9EHunSg=; b=JYbStqWKiLVP8gQb8QrQont1MA+fVOebzoNCXhhChNRhLA66gjkz6iCtjK1sSXoFf7 e9GMA2LKo/kAXnVFeODnQ42G0gNCA9mGGu7+Esgh1kD5dYAINNZrFxKJu1gH+mn2OW/g m/gy4nAS6WUnR/IDCsIbgC+rQBie9gbfWy3ru0amYbV0jZzLWNKI4T9XJLLWWxboPRlw INgAQ2I2ovQ3GV5Hi9MwW7NUxr1rNDmAyKnPXrysvGQI7JeWKDH7haSpkL16ya9vfC6R yJ1y4QDbokJItF80fwuqcYLiKTYTaHjnhf7OxJaaZjHse1CfetZ41kb7Uzck/vlYRTIn LtAA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680099970; x=1682691970; 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=yI53fJe3tfM7C7UI3uCUCQiatoboVbtQcUEC9EHunSg=; b=1h/B3UYiGgvlJ6RtIHXUsgt6qcBoFucRH1RIa5NcaoS6SwX/hZrcuUuS1mMA/20qLl XaroIgmegHDXJ2mhF7pGW1iI7i0B5V6l8dONaxEjL+YxCG2sSQdI8OijbYn5rklvlTnl joSD6qk4NP1fZxwqxlB0sg/CDNG8JGoctc4iR1btXwV29AkUmz6LKDF0/IB8p4wNAzxB FwQMcP71dP8+x/P9ZJqAH+Mnej4jA0f00P65eHFqb37OMN8Cm23GNFS7ChD7icefRZTc UcMao6escyH7p93xwc7SJm/hsGzMUOMyzLzt8bYB9xvTy90mxkL74s5nLWsCY8nRA/OH kW2Q== X-Gm-Message-State: AAQBX9dENEGTX6tYy+xTP7wGO84eBSz0T7yPu/QhvMhEqEEHNl5/ycfR gfq4l7YOmQYRCdDHNvkNjd2bXxVrzNW7WDjwRW67MY3x X-Google-Smtp-Source: AKy350YHQaDyYSiyFYBuG00J7UXLIxX4HKnF4Phh+ui6rCIO1VDF63dkJPMZgynp4lf5eEBxiPclbYxgD1CrjwiUPAg= X-Received: by 2002:a1f:94c2:0:b0:43b:ce2e:89f5 with SMTP id w185-20020a1f94c2000000b0043bce2e89f5mr5814796vkd.3.1680099970008; Wed, 29 Mar 2023 07:26:10 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Date: Wed, 29 Mar 2023 17:25:52 +0300 Message-ID: To: Rowan Tommins 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: rokas.sleinius@gmail.com (=?UTF-8?Q?Rokas_=C5=A0leinius?=) First, I'm pretty sure I agree now that enums should *not* be `extend`-ed as regular classes, there's a fundamental difference as you state - the extended enum is a fundamentally different THING as it can hold different values - when an enum's sole purpose is to hold one of the set of values. That is to say - for most implementations that's the desired case. I'll refer to the solution to the OP problem as ECS, Error Code System. For ECS, the most elegant, but also the only, solution I can come up with - is the kind of enum that you also suggest, a `base-enum`. `abstract enum`? Let me try again to describe the problem that I came here with that is unsolvable by union types: 1. ECS is a *generic* component. I want to plug it into any working system (like an existing cms). 2. Invoking it must provide a unique ErrorCode. The existing cms has to create an enum that will be accepted as `type: ErrorCode`. It cannot edit the existing ErrorCode as it is part of ECS and not the cms. 3. There is NO special handling for the custom ErrorCode's that the users of ECS create. We are just using the enums inferred value. I will keep processing this situation further, but for now it seems to me l= ike 1. Extending enums is fundamentally flawed. 2. Basing enums off of other enums has valid usage scenarios. In that case, and, again, this needs way more thought to it, it's not such a "generic way forward" that seemed to me at first and might only provide marginal value at the cost of type complexity and that is most probably, unfortunately, not worth it... (unless `abstract enum` might make sense, but my brain needs some time off of this problem for now) Thank you for such a thought out discussion, everyone! On Wed, 29 Mar 2023 at 16:56, Rowan Tommins wrote= : > > On Wed, 29 Mar 2023 at 14:22, Rokas =C5=A0leinius wro= te: > > > Ok so I am trying to find the argumentation here: > > > > >This is by design. > > >The point of enums is to be limiting. > > > > This is clearly an assumption. That statement is not in the docs or > > RFC, nor such an oversimplification makes logical sense to me, but > > maybe you have a source to back it up..? > > > > > From a theory point of view, *any* type definition is about limiting > allowed values. The difference between "mixed" and "integer" is that > "mixed" allows both 'Hello' and 42, but "integer" does not - it defines a > tighter limit. > > In the same way, saying "I accept an error code" means "I accept an error > code *and nothing else*" - the definition of what is and what isn't an > "error code" is deliberately a *limit* on the values that you accept. > > > > > > > >Re: problem in the OP > > >You are approaching the design of this in a fundamentally wrong way. > > > > The described problem is dealing with error *codes* not *types* > > > > An enum is a type, so that's why George was talking about types. He was > making the same point, in different words, as I did: the relationship > between types implied by the keyword "extends" is not the relationship yo= u > want in this case. > > > > > The set in question is *finite*, as you say of course, but much larger > > and different in principle than in the solution you are proposing. > > Problem in OP is looking for a way for end users to keep adding new > > domains to the error codes passed to the parent system to be handled. > > > > > This is the key point - if you pass an error code that didn't previously > exist, the existing system *won't* be able to handle it. > > That's why enums are inherently restrictive: the system wants to be able = to > say "this is the list of error codes I understand, don't give me anything > else". > > If you have a system that accepts the new codes as well as the old ones, > you can use a union type declaration as George says: > > enum AdditionalErrorCode { > case Code_456; > } > > class ExtendedErrorHandler extends StandardErrorHandler { > public function handle(StandardErrorCode|AdditionalErrorCode $code) { > switch ( $code ) { > case AdditionalErrorCode::Code_456: > // handling for new code goes here > break; > default: > // assert($code instanceof StandardErrorCode); > parent::handle($code); > break; > } > } > } > > Regards, > -- > Rowan Tommins > [IMSoP]