Re: [RFC] [Under Discussion] Auto-implement Stringable for string backed enums
| From: | Benjamin Eberlei | Date: | Wed, 22 Jun 2022 12:33:41 +0000 |
| Subject: | Re: [RFC] [Under Discussion] Auto-implement Stringable for string backed enums | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-118045@lists.php.net to get a copy of this message | ||
On Wed, Jun 22, 2022 at 12:47 AM Nicolas Grekas <
nicolas.grekas+php@gmail.com> wrote:
> Hi everyone!
>
> I'd like to open a discussion on this RFC, to auto-implement Stringable for
> string-backed enums:
>
>https://wiki.php.net/rfc/auto-implement_stringable_for_string_backed_enums
>
> I'm looking forward to your feedback,
Hi Nicolas, Hi Ilija,
I would prefer if this was an explicit opt-in to have a __toString on a
backed enum. Maybe a special trait for enums that has the implementation,
so that a custom __toString cannot be implemented or a new syntax "enum Foo
: Stringable".
My concern is that auto implementing __toString, will lead to decreasing
type safety of enums in weak typing mode, since they get auto-casted to
string when passed to a function accepting strings. This effectively adds
more type juggling cases.
The example in the RFC about attributes accepting strings or Enums can be
solved by union types on the side of the library developers, it doesn't
need to be magically implemented by the engine.
I don't consider "use strict mode" a good argument to avoid this problem,
because that has other downsides such as overcasting.
>
> Cheers,
> Nicolas
>