Re[2]: Fwd: Re[2]: [PHP-DEV] PHP 8.1 enum con st expressions problem
| From: | Kirill Nesmeyanov | Date: | Fri, 30 Jul 2021 17:24:21 +0000 |
| Subject: | Re[2]: Fwd: Re[2]: [PHP-DEV] PHP 8.1 enum con st expressions problem | ||
| References: | 1 2 3 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-115613@lists.php.net to get a copy of this message | ||
>Пятница, 30 июля 2021, 16:45 +03:00 от Rowan Tommins
><rowan.collins@gmail.com>:
>
>On 30/07/2021 13:20, Kirill Nesmeyanov wrote:
>> But in addition to bit-masks, there are also at least cases of addition and subtraction
>> (the most popular). Can't we adapt all math expressions existing in nature to specific cases,
>> thereby building our own pseudo-AST?
>>
>> (Some::A + Some::B) * Some::C | Some::D;
>
>If you are performing arithmetic on a value, that value is not of an
>enum type, or at least not in the way the current feature defines "enum".
>
>If you write an enum for days of the week, you might give them integer
>values, such that "Day::MONDAY->value === 1" and "Day::SUNDAY->value ===
>7". However, expressions such as "Day::SATURDAY + Day::SUNDAY" and
>"Day::MONDAY * Day::TUESDAY" are clearly meaningless.
>
>At a stretch, a language with operator overloading might define
>"Day::MONDAY + 1" to return "Day::TUESDAY", and "Day::MONDAY + 10"
>to
>either error or wrap around to "Day::THURSDAY"; but PHP has covered that
>use case by allowing methods, e.g. "Day::MONDAY->advanceBy(10)", which
>is more flexible and arguably more expressive.
>
>On the other hand, the expression "$weekend = Day::SATURDAY |
>Day::SUNDAY" seems reasonable; but we don't need the result to be an
>integer - it would have no meaning on its own - we just need to be able
>to ask things like "is $today in $weekend?" Returning some kind of
>EnumSet object means we retain the type information, and can keep our
>values of 1 to 7 (or have no values at all) rather than having to use
>powers of 2.
>
>Regards,
>
>--
>Rowan Tommins
>[IMSoP]
>
>--
>PHP Internals - PHP Runtime Development Mailing List
>To unsubscribe, visit: https://www.php.net/unsub.php
1) What about this example?
enum ErrorGroup: int
{
case NOTICE = 0;
case WARNING = 10;
case ERROR = 20;
}
enum ErrorCode: int
{
case UNKNOWN = 0;
case A = ErrorGroup::NOTICE + 1;
case B = ErrorGroup::NOTICE + 2;
case С = ErrorGroup::NOTICE + 3;
case D = ErrorGroup::WARNING + 1;
case E = ErrorGroup::ERROR + 1;
}
The same applies to, for example, designing Http statuses (However, since they are pre-specified,
this does not make sense).
2) OR: As an example from real life, I can show this pseudo-enum: https://github.com/SerafimArts/ffi-sdl/blob/master/src/Kernel/Video/PixelFormat.php
How is this supposed to work in the future? Bit masks are just one case of many others.
--
Kirill Nesmeyanov