Re: Let's discuss enums!
| From: | Rowan Collins | Date: | Fri, 18 Sep 2015 21:30:17 +0000 |
| Subject: | Re: Let's discuss enums! | ||
| References: | 1 2 3 4 5 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-88360@lists.php.net to get a copy of this message | ||
On 18 September 2015 15:42:44 BST, Anthony Ferrara <ircmaxell@gmail.com> wrote:
>As far as enums, I wonder if they would be necessary if we supported
>algebraic types, since you could define an "enum" as a virtual type:
>
>const MONDAY = 0;
>const TUESDAY = 1;
>const WEDNESDAY = 2;
>const THURSDAY = 3;
>const FRIDAY = 4;
>use MONDAY | TUESDAY | WEDNESDAY | THURSDAY | FRIDAY as WEEKDAY;
>
>function foo(WEEKDAY $day) {
>// must be an integer 0-4, or castable to 0-4 unless strict_types is
>on.
>}
I think this is a fundamentally different style of enum to what Levi and I are thinking of.
In your example - and this seems to be what C# means by enum, for instance - it's a subset of a
scalar type, usually an integer, with some names as mnemonics. Other than that it behaves exactly as
the base type would, and can be readily cast, maybe even implicitly, to and from that base type.
The other style is where each enum type is a completely distinct type, not just a whitelist of
values in some other domain. In a sense (and this is what got me thinking about enums recently), you
can consider null to be an enum with only one value, and boolean to be an enum with two - you can
define (int)true as 1, but true is not an integer, and 1 is not a boolean value.
I suspect both styles may have their place, but algebraic types certainly wouldn't take away my
thirst for object-like enums.
Regards,
--
Rowan Collins
[IMSoP]