Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:118054 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 35523 invoked from network); 22 Jun 2022 14:11:55 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 22 Jun 2022 14:11:55 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id AC84518054C for ; Wed, 22 Jun 2022 09:01:26 -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=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,HTML_MESSAGE, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS, 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-yb1-f177.google.com (mail-yb1-f177.google.com [209.85.219.177]) (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 ; Wed, 22 Jun 2022 09:01:26 -0700 (PDT) Received: by mail-yb1-f177.google.com with SMTP id u9so20816295ybq.3 for ; Wed, 22 Jun 2022 09:01:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=1sNbLxYRKsQriwRmXk1zG4NGgIo+HKeBNWgRl3b357U=; b=HlXinzMrBB89qFtqaqi1ER37J98/SEFNbiActLdqWV7RM81dHlONOpi6YoXpBxTd7j pWoeDZtPBOqf0P/hDUiGuVj7yCHe7Ppzf2h9BJSRNMV8AUXFov7Ty8qqAUADEsy9yngE ToRhfcfAhFdaiSHC/lf6Kd+BUSOUFrGfOEcrxHWkKCRPlBko0dWeTKw/aJA2AFKBs8uY AZ+CJp6lsuDbcc5ZOisWSNmcy1VqTcHZZmGD+r3nYLs91ZlJLxUFZ6PV/OS0F3Xppb9V 5Lr61y/PlkVIjR12K2/aSt1vVW1Wzqyam8kYUxnNqjOC8FuuHvRnKWAr2wzysfrQwC3N TGLQ== 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=1sNbLxYRKsQriwRmXk1zG4NGgIo+HKeBNWgRl3b357U=; b=3K1nB8L8CHb8rcP3Ea5pREYhvJO+2j5iRIWAfhD/A8v0HCRegid2w1nVRPRU7MZXSg kac9N84wLRHxQMdQV3Se1Fi8xH3oT1EccqUFb+HHR9ecoF84cCY6Kp65J1nWRkOzaE7w unHo7zJE6wr4OHSqyIjqzOFUmvy+WPY/0yhP8Ms+yFR1DxrBnBAcBhnZxVlJHCHFBeQy FwaaGmGyEYoiDK/7mfXkKsGhXV1hIgzUiXKPLu5N21WbgFsH4IILChzZnad35D37tIMU CQGF7SrSduhAEHc8Af5VThGegPIX/KWFrOL5Nu6n1b5pUGuCwhfS4F4hRLEgkKGZMqBj twPw== X-Gm-Message-State: AJIora+Wapv3PO85eHZSJMtlrDikIgB84m/V/MMhXz26YI9cuYN9WYjV NuZ3AtDQij4lAtrwAqaIrItwHvOL3c43mSpzsYM= X-Google-Smtp-Source: AGRyM1s1M306zye7YZ1sjYTSa2mZFmW3AGsZ/nWd12h6Pym00E0q9viA6txpN9BqHoILiT9Q4iI9Q0Sf4bLVSP/XeFM= X-Received: by 2002:a25:9d89:0:b0:669:31d4:7cd9 with SMTP id v9-20020a259d89000000b0066931d47cd9mr4548970ybp.294.1655913685266; Wed, 22 Jun 2022 09:01:25 -0700 (PDT) MIME-Version: 1.0 References: <9C11261B-B9D0-4342-81EB-60276B3036E5@php.net> In-Reply-To: <9C11261B-B9D0-4342-81EB-60276B3036E5@php.net> Date: Wed, 22 Jun 2022 18:01:13 +0200 Message-ID: To: Derick Rethans , Benjamin Eberlei Cc: PHP Internals List Content-Type: multipart/alternative; boundary="000000000000fd35f405e20b7213" Subject: Re: [PHP-DEV] [RFC] [Under Discussion] Auto-implement Stringable for string backed enums From: nicolas.grekas+php@gmail.com (Nicolas Grekas) --000000000000fd35f405e20b7213 Content-Type: text/plain; charset="UTF-8" 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 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 --000000000000fd35f405e20b7213--