(CC'ing internals as it was apparently dropped by Jeff)

On Fri, Sep 18, 2015 at 11:03 AM, Levi Morrison <le...@php.net> wrote:
> On Fri, Sep 18, 2015 at 9:59 AM, Jefferson Gonzalez <jgm...@gmail.com> wrote:
>> 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.
>
> According to type theory the enumerated type is just a special case of
> the union type where each type constructor in the union takes exactly
> zero parameters (meaning they are all singletons). So… what is the
> purpose of an enumerated type? To create a finite set of values that
> are all unique. That's it – that's the whole purpose.
>
> Anything else is trying to add value in some way – in your case you
> are adding a primitive value that the language will automatically map
> for you. I'm not saying that is a bad thing but I hope you can at
> least recognize that it is an addition and not the bare minimum. The
> RFC I am planning on proposing will have only the bare minimum because
> it's the least controversial part and is the most useful. The other
> stuff we can debate about later.

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

Reply via email to