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.

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to