On 09/18/2015 10:21 AM, Levi Morrison wrote:
On Fri, Sep 18, 2015 at 1:05 AM, Pavel Kouřil <pajou...@gmail.com> wrote:
On Fri, Sep 18, 2015 at 1:06 AM, Rowan Collins <rowan.coll...@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.
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
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php