Newsgroups: php.internals,php.internals Path: news.php.net Xref: news.php.net php.internals:88332 php.internals:88333 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 67924 invoked from network); 18 Sep 2015 15:28:31 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 18 Sep 2015 15:28:31 -0000 X-Host-Fingerprint: 24.41.170.168 unknown Received: from [24.41.170.168] ([24.41.170.168:8468] helo=localhost.localdomain) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id B5/51-60254-E9D2CF55 for ; Fri, 18 Sep 2015 11:28:31 -0400 To: internals@lists.php.net,Anthony Ferrara , Levi Morrison References: <55FB4791.4040807@gmail.com> Cc: John Bafford , Bob Weinand , Rowan Collins , internals Message-ID: <55FC2D97.80703@gmail.com> Date: Fri, 18 Sep 2015 11:28:23 -0400 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.2.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Posted-By: 24.41.170.168 Subject: Re: [PHP-DEV] Let's discuss enums! From: jgmdev@gmail.com (Jefferson Gonzalez) 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.