Re: Let's discuss enums!

From: Date: Fri, 18 Sep 2015 15:28:23 +0000
Subject: Re: Let's discuss enums!
References: 1 2 3 4 5  Groups: php.internals php.internals 
Request: Send a blank email to internals+get-88333@lists.php.net to get a copy of this message
On 09/18/2015 10:42 AM, Anthony Ferrara 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.
} Just a thought... Anthony
Thats the kind of functionality I would expect from enums, but having a dedicated syntax for it would be better for code organization and documentation. Is much better to document: /** * Days of the week for blah blah. */ enum Weekday {...} Its purpose would be much clear than documenting this: use MONDAY | TUESDAY | WEDNESDAY | THURSDAY | FRIDAY as WEEKDAY; Also documentation generators like phpdocumentor or apigen could generate a list of enum declarations with its descriptions and values. Also when type hinting a function parameter like in your example it can be a link to the enum definition on documentation.

« previous php.internals (#88333) next »