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