Re: [RFC] [Under Discussion] Auto-implement Stringable for string backed enums
| From: | Guilliam Xavier | Date: | Mon, 27 Jun 2022 12:03:54 +0000 |
| Subject: | Re: [RFC] [Under Discussion] Auto-implement Stringable for string backed enums | ||
| References: | 1 2 3 4 5 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-118104@lists.php.net to get a copy of this message | ||
>> Symfony YAML has a
!php/const X feature, which also works
>> when X is
>> an Enum::CASE; how about a !php/enum_value feature?
>
> I submitted something similar today at
> https://github.com/symfony/symfony/pull/46771
And I see that it has been merged ;)
>> Otherwise, I also like Rowan's suggestion of implementing "internal
>> cast handlers", so that non-strict users could call e.g.
>
> I had a look at gmp for example: cast handlers don't work when calling a function.
> They do work when explicit casting and when doing loose comparisons, but they don't when
> calling functions
Well it works for string at least (even though GMP does *not*
implement __toString), e.g. https://3v4l.org/cRnnW
The TypeError for int may be related to the fact that e.g. +$gmp
(or 0 + $gmp) gives back a GMP (not an int like (int)$gmp)?
(But indeed I don't know much about that... nor if it could be made opt-in)
> Rowan's proposals about sets could solve this in a very nice way
Reminded me of e.g.
https://stackoverflow.com/questions/6422380/does-any-programming-language-support-defining-constraints-on-primitive-data-types
(mainly integer ranges, but the concept of "domains" looks similar)
Regards,
--
Guilliam Xavier