Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:88337 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 77464 invoked from network); 18 Sep 2015 15:59:42 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 18 Sep 2015 15:59:42 -0000 X-Host-Fingerprint: 24.41.170.168 unknown Received: from [24.41.170.168] ([24.41.170.168:23231] helo=localhost.localdomain) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 31/53-60254-DE43CF55 for ; Fri, 18 Sep 2015 11:59:42 -0400 To: internals@lists.php.net,Levi Morrison , =?UTF-8?Q?Pavel_Kou=c5=99il?= References: <55FB4791.4040807@gmail.com> Cc: Rowan Collins Message-ID: <55FC34E6.8020009@gmail.com> Date: Fri, 18 Sep 2015 11:59:34 -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: 8bit 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:21 AM, Levi Morrison wrote: > On Fri, Sep 18, 2015 at 1:05 AM, Pavel KouĊ™il wrote: >> On Fri, Sep 18, 2015 at 1:06 AM, Rowan Collins wrote: >>> Hi All, >>> >>> This has come up in passing a few times recently, but I'm not sure there's >>> ever been a dedicated discussion of it: would it be useful for PHP to have a >>> built-in Enumeration type, and if so, how should it look? >>> >>> Many other languages have enum types, with varying functionality. The >>> central concept is always that there's some declared type with a bunch of >>> named constants, but beyond that there's a lot of variability: for starters, >>> is it a type of object, a special way of naming integers, or an uncomparable >>> type more like booleans and nulls? >>> >>> >>> So, what are anyone's thoughts? Am I rambling on too much as usual? ;) >>> >>> Regards, >>> >>> -- >>> Rowan Collins >>> [IMSoP] >>> >> >> Hi, >> >> personally, I feel that you should be able to assign numeric value to >> each element (and have them implicit if not specified). >> >> This is imho better for serialization (but it can be done with names >> as well, yeah) - but more importantly, it also allows usage with >> bitwise operators, so you could use them as a "flags" (ie. $weekend = >> Days::SATURDAY | Days::SUNDAY). > > In my opinion this is the least valuable form of enums that exists in > any language I am aware of. However I was careful in the RFC to not > prevent this from being a possibility. I would much, much prefer enums > that are more like Rust's, Haskell's or Swift's. It's worth noting > Swift has at least three different kinds of enums, one of which would > allow the kind of behavior you are wanting. > It may not be valuable in your opinion, but I have seen this kind of enum usage on a lot of open source projects which is really useful to represent more than 1 value in a single variable, which would be more efficient and less memory consuming than doing something like: $weekend = array( Days::SATURDAY; Days::SUNDAY; ) And anyway, most new languages have over engineered the purpose of enums. Enums should just be a nice way of grouping constant values with a human readable name where the ordinal value should be optionally set for consistency in case that storing it is required. In any case this is a nice read: https://en.wikipedia.org/wiki/Enumerated_type