Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:112724 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 66134 invoked from network); 3 Jan 2021 11:26:20 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 3 Jan 2021 11:26:20 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 357D61804DD for ; Sun, 3 Jan 2021 03:01:59 -0800 (PST) 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.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL, SPF_HELO_NONE,SPF_NONE autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from mail-qt1-f181.google.com (mail-qt1-f181.google.com [209.85.160.181]) (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, 3 Jan 2021 03:01:58 -0800 (PST) Received: by mail-qt1-f181.google.com with SMTP id u21so16619020qtw.11 for ; Sun, 03 Jan 2021 03:01:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=newclarity-net.20150623.gappssmtp.com; s=20150623; h=mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=OtDNkNG+UYnu2NmhoBiJHtz34YfeK5Oc2Fpq9ghOFsw=; b=Xo6z2QRFddwzNpP7NxCzXc4RjZ6ijD5A5C6FLcgAFeyipJG/af2+7Nld8nUJqPy+mH 8vxM5zQnsqdCNRe9srA/zReb9Jy1I6J5pz104+p080kXHxdUAYOuMLUic8dnlO0s7oRI /k28CKmip4H2E4UvK+gg2/ngoYdJXejyrGFiFz9zegvwU5dBtz/mqvBjX8XdMK4eDS2u iqefPGxi3C9/xEqhWWowYPtCRcMqWGsEFb7ouKvslFs1EQ0dw4RtwJXypGydWso1QSMe sSnYYDz12CoprP0xZdRuEKfk2pYuxRi82ijSQn6tu+xaST+/Oj6Uma5GvOB6NbXw0E0h JnFA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=OtDNkNG+UYnu2NmhoBiJHtz34YfeK5Oc2Fpq9ghOFsw=; b=n6oOtGw1xRFitH5sW5jjnJCHwLYRDeSgB+5ZNBK3bKh0YFkNjmQiNL6sJlZ8XO5mIS 5MGq1o6BIgriy87JlLobUZYR46YfqQ9SDmlCdhvtDf5znTY1GGHmpJ/zR9knvzr12bea lY5RzJRRipr0jUy5guFpXvOCJ79V7N9O/fxocbZ7nhqpGqGok8uSqdUmalgtPWYBmNvR YPxab5Pz/3YnjpZG9nJeN/cgJRbb5jIDe/ErmpLH5dXv7SM1wQh2PHK5KMvOARSF0LPW vvdnlki0myK8aA9bc4vZl7Aa4zQP1loovIdeLs3I7d+iv43JDY8ggfsW1Ki2EUPnM05r 7rbA== X-Gm-Message-State: AOAM531xYqbB83f86kCf67C+M1SSj5B1o5HezXqp0VE0bHU/Ftv9GfWI mGT/TDv+2VksSTVKQYKDMxH+fp6Y2A7BaA== X-Google-Smtp-Source: ABdhPJzTz9EZqkOn3iFv6A92JigcycdFlWWGmYXkEylgoHriUWuvaeX9k9M6YcVhJC6GEAbXpifnZA== X-Received: by 2002:ac8:5806:: with SMTP id g6mr67302049qtg.292.1609671716579; Sun, 03 Jan 2021 03:01:56 -0800 (PST) Received: from [192.168.1.239] (c-24-98-254-8.hsd1.ga.comcast.net. [24.98.254.8]) by smtp.gmail.com with ESMTPSA id s14sm15924846qke.45.2021.01.03.03.01.55 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sun, 03 Jan 2021 03:01:55 -0800 (PST) Content-Type: text/plain; charset=us-ascii Mime-Version: 1.0 (Mac OS X Mail 13.4 \(3608.120.23.2.4\)) In-Reply-To: <7d8ce2a2-8d85-4312-af22-da643faa3a7f@www.fastmail.com> Date: Sun, 3 Jan 2021 06:01:54 -0500 Cc: php internals Content-Transfer-Encoding: quoted-printable Message-ID: <1BC8BF20-B961-4360-855B-5BB95338BB8A@newclarity.net> References: <8aa05350-05fc-df9d-e5d6-fa0f4feb57ba@alec.pl> <4eec7448-f7ab-9955-8c2d-68cd4f822535@gmail.com> <7d8ce2a2-8d85-4312-af22-da643faa3a7f@www.fastmail.com> To: Larry Garfield X-Mailer: Apple Mail (2.3608.120.23.2.4) Subject: Re: [PHP-DEV] [RFC] Enumerations, Round 2 From: mike@newclarity.net (Mike Schinkel) > On Dec 31, 2020, at 12:15 PM, Larry Garfield = wrote: >=20 > On Thu, Dec 31, 2020, at 6:53 AM, Rowan Tommins wrote: >> On 30/12/2020 21:24, Aleksander Machniak wrote: >>> My argument is that, from an end-user perspective, I don't really = see >>> why Unit and Scalar enums have to have different "API" at this = point. >>> I'm talking about ":string"/":int" in the enum definiton as well as >>> ->value and ->from(). >>=20 >>=20 >> My personal opinion is that for many enums, explicitly not having a=20= >> scalar representation is a good thing. >>=20 >> This is basically similar to my opinion of __toString() etc: if you = have=20 >> *multiple* ways to convert something to/from a scalar, blessing one = of=20 >> them as "default" is arbitrary and confusing. >>=20 >> For example: >>=20 >> enum BookingStatus { >> case PENDING; >> case CONFIRMED; >> case CANCELLED; >>=20 >> public function getId() { >> return match($this) { >> self::PENDING =3D> 1, >> self::CONFIRMED =3D> 2, >> self::CANCELLED =3D> 3, >> }; >> } >> public function getCode() { >> return match($this) { >> self::PENDING =3D> 'PEN', >> self::CONFIRMED =3D> 'CON', >> self::CANCELLED =3D> 'CAN', >> }; >> } >> public function getEnglishDescription() { >> return match($this) { >> self::PENDING =3D> 'Pending Payment', >> self::CONFIRMED =3D> 'Confirmed', >> self::CANCELLED =3D> 'Cancelled', >> }; >> } >> } >=20 > That is similar to our reasoning. It creates a foot-gun situation = where someone could get in the habit of assuming that an enum always has = a *reasonable* and *logical* and thus *reliable* string equivalent, when = not all enums will have string equivalents that it's reasonable and = logical to use. So, one less foot gun. However, avoiding one foot-gun does not always mean that you avoid all = foot-guns. For example, when you need to create a large number of string enums = where the name and the symbol are the same, it would be very easy to = have a typo in one but not the other, especially as a result of copy and = paste editing. So in my perfect world this: enum BookingStatus { case PENDING; case CONFIRMED; case CANCELLED; } Would be equivalent to: enum BookingStatus { case PENDING =3D "PENDING"; case CONFIRMED =3D "CONFIRMED"; case CANCELLED =3D "CANCELLED"; } #fwiw >=20 > Also, one of the extensions planned, as noted, is ADTs/tagged unions. = Those could not have a primitive equivalent, since they're not = singletons. Keeping UnitEnum and ScalarEnum separate allows us to later = add TaggedEnum (or similar) that also extends UnitEnum, but not = ScalarEnum. >=20 > --Larry Garfield >=20 > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: https://www.php.net/unsub.php >=20