Re: Let's discuss enums!

From: Date: Fri, 18 Sep 2015 14:21:53 +0000
Subject: Re: Let's discuss enums!
References: 1 2  Groups: php.internals 
Request: Send a blank email to internals+get-88325@lists.php.net to get a copy of this message
On Fri, Sep 18, 2015 at 1:05 AM, Pavel Kouřil <pajousek@gmail.com> wrote: > On Fri, Sep 18, 2015 at 1:06 AM, Rowan Collins <rowan.collins@gmail.com> 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.

« previous php.internals (#88325) next »