Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:118068 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 2466 invoked from network); 23 Jun 2022 06:17:48 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 23 Jun 2022 06:17:48 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 751A41804B1 for ; Thu, 23 Jun 2022 01:07:30 -0700 (PDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HTML_MESSAGE,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2, SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS15169 209.85.128.0/17 X-Spam-Virus: No X-Envelope-From: Received: from mail-wm1-f43.google.com (mail-wm1-f43.google.com [209.85.128.43]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Thu, 23 Jun 2022 01:07:29 -0700 (PDT) Received: by mail-wm1-f43.google.com with SMTP id m125-20020a1ca383000000b0039c63fe5f64so991131wme.0 for ; Thu, 23 Jun 2022 01:07:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=beberlei-de.20210112.gappssmtp.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=XytPT/Dfw1I64Z8mMOWH6Fo7tzfl1ewPsEnvyd1eXLY=; b=NwvSKyydXIXVfZnEhRXIZBMKIo3DOFPJKi3QMJHkg1WgP4z+Ov2LkzRCzepVzdG45T SRkK9rSE5ieVXtzQuUv6EaifjEZlANanh+pNfeoolrSJ0rHyWpxaifCHYUBfBkDdSZXI DEZ+MHB6oT3O1+cyov54BrdoLMBh9e6OF+MYVXdqsf9fwaoI7kMpfGenyZJY1cWO1Bvz ed7Zq3gLHQCg3XjDwcnehavC/ykrfgBFCl0YkG7hI3jX/TOUMmcxyJCYwxbRYJsebkCv zl5fHY26qrMUEuzeP+Tj9bmRKQ6vGHLQFPuK2oBehVq0F68Mjp/VCHdKZrxFTRQY8JHw eT0Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=XytPT/Dfw1I64Z8mMOWH6Fo7tzfl1ewPsEnvyd1eXLY=; b=oxk/9fL5qsaVFh/BAJbaoxLyiZbBWvPHnHkSB8yQ0zANGkIhcJ/ULJmPw0HxyDVKP3 qmfJFLI8oUcLWz+D8LH0oU4sXgHlek46d0TuyGl5DqLlYJQzX10dQQh6XG1qNoQdo8/G mASIIwm1E0RAGxIVIOsfUTntwImtVwTCv0L0D6MVBbY6M3t3Gny2480qpWZmMjBFGCxD ssDrl7hooUgDzOr+/z3x6Rm5P69lHatiNSFmAUkfb2WlYJxdCMswXubzMqg4gyoQZ7kd NyZhPKlohYNEtl94svQbBjHNLtoU5FymdMW55l3XRQpfwYBtDVvN9nkgCg/lQE0LuLVf qsxg== X-Gm-Message-State: AJIora/gf8RVTjoVpXnrE867/v0uWuOTp62GqDfVr3m5LI8UoCmSMb0A 9MeLpz4qCmKQyD3xWPaplJG/W/hPvu+24oi2oLfa0ns3cBpNgw== X-Google-Smtp-Source: AGRyM1vv1+nER5h/5h2AA8SPSfz6mbcSLxJYEoU9gSE7pR32wmxgGCO7eOZg9W551WXQL3ELlcjNiqIGbpjwMD621nA= X-Received: by 2002:a05:600c:501f:b0:39d:a3d:e919 with SMTP id n31-20020a05600c501f00b0039d0a3de919mr2667339wmr.132.1655971648308; Thu, 23 Jun 2022 01:07:28 -0700 (PDT) MIME-Version: 1.0 References: <9C11261B-B9D0-4342-81EB-60276B3036E5@php.net> In-Reply-To: Date: Thu, 23 Jun 2022 10:07:17 +0200 Message-ID: To: Nicolas Grekas Cc: Derick Rethans , PHP Internals List Content-Type: multipart/alternative; boundary="000000000000db1a1805e218f11a" Subject: Re: [PHP-DEV] [RFC] [Under Discussion] Auto-implement Stringable for string backed enums From: kontakt@beberlei.de (Benjamin Eberlei) --000000000000db1a1805e218f11a Content-Type: text/plain; charset="UTF-8" On Wed, Jun 22, 2022 at 6:01 PM Nicolas Grekas wrote: > Hi Benjamin and Derick, > > I'm replying to both of you because I see some things in common in your > comments. > > > >> https://wiki.php.net/rfc/auto-implement_stringable_for_string_backed_enums > > > 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". >> > > We can make this opt-in by simply allowing user-land to implement > Stringable. > This is a different solution to the problem the RFC tries to solve and I > would also personally be fine with. > I would then *not* try to limit which implementation of it is allowed by > the engine. Instead, I would give all powers to user-land to decide on > their own what makes sense for their use case. As a general principle, I > believe that empowering user-land is always a win vs trying to "save them > from themselves", as if we knew better than them what they want to achieve, > and especially how. > > >> 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. >> > > I don't share this concern: if an API accepts a string and the engine can > provide a string, let it do so. There is nothing inherently dangerous in > doing so. But we don't need to agree on that if the proposal above makes > sense to everybody :) > > >> 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 extensively explain in the RFC why this should not be on the side of lib > authors, but on the side of end-users. Please double check and let me know > your thoughts. > I don't fully agree with your argument in the RFC. You mention as an exmaple that users want to pass PossibleRoles enum values into the IsGranted attribute. But the way Symfony works as you know, you can pass arbitrary role names here from the library POV, and applications can add their own. So naturally the API is to pass a string. So "PossibleRoles" can only be a user provided enum, it can never be a Symfony type. I don't see how PHP should provide a workaround for Symfony and its users wanting to allow users to use an arbitrary enum as input to a function. In this example, in Symfony code you only expect "any" BackedEnum and cannot validate that it is a value of PossibleRoles enum. As such Rowan suggestion to use constants is the way Symfony should recommend their users. > >> I don't consider "use strict mode" a good argument to avoid this problem, >> because that has other downsides such as overcasting. >> > > I'm 100% aligned with that. > > 2. It is not implemented for all enum types, only for string backed ones. >> This change would now make different classes of enums, without them being >> differently inherited classes. >> > > This would also be solved by allowing user-land to implement Stringable on > *all* kind of enums. Would that make sense to you? > > >> 3. The main use case seems to be to prevent having to type ->value, which >> would otherwise indicate reliably whether an enum is used as string >> argument. >> > > Yep, that's a "strict mode" approach and this RFC mostly applies to > non-strict mode. > There are also cases where using "->value" is just not possible. I mention > attributes in the RFC, but we also have a case in Symfony where defining > service definitions in yaml doesn't work with enums because there is no way > to express the "->value" part. > > If that's the consensus, I'm fine updating the RFC to turn the vote into > whether "allowing user-land to implement Stringable on any kind of enums" > is desired or not. > > Nicolas > --000000000000db1a1805e218f11a--