Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:122324 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 57373 invoked from network); 7 Feb 2024 10:21:21 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 7 Feb 2024 10:21:21 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1707301334; bh=UXd/I17qPacBXMNBAgRU2j30wI4lnTBl4RWw7DYXRwk=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=azAvB+o4ldNw9ok9J80hbasRJlAYzlW565ulx6PB4q+MgOaAX6XdfyT16cqKccScF 7r7Yfb2U2sn7lL9dyfUw9klZglPcMCKRUy7mgGJOrDrhuv/iEPcHM9oR1JtWr4hHg3 gdiZHMxoa4P9e5+t/JyWMyGp4IHdf1Yn9ffPlMurb6M66AB1L9KJnD/VPaYoP4g3lP Z7bnB6ZlgQF+doGGm8YryHgxypUM1jtyDf8k5w/2VuW4qJK21z3x4aXfK12GdNJRdx PVsrHEt9R9L7y9wtnU9fIdUHNMEL425542Ueq7MxgjyjiMSSErsu022mY98Qs8Hzu9 Dsl2e3bDCmZLA== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 063B4180069 for ; Wed, 7 Feb 2024 02:22:11 -0800 (PST) X-Spam-Checker-Version: SpamAssassin 4.0.0 (2022-12-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-0.6 required=5.0 tests=BAYES_00,BODY_8BITS, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_PASS, FREEMAIL_FROM,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL, SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: No X-Envelope-From: Received: from mail-oo1-f47.google.com (mail-oo1-f47.google.com [209.85.161.47]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Wed, 7 Feb 2024 02:22:10 -0800 (PST) Received: by mail-oo1-f47.google.com with SMTP id 006d021491bc7-59a9a737273so115445eaf.1 for ; Wed, 07 Feb 2024 02:21:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1707301277; x=1707906077; darn=lists.php.net; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=Ol9S0atgXTRMlRPwMU7mGPSjUu7OjdesfDZweTnFUmI=; b=BuWcgWhjFbnIqNhwLpQH+9fJRb9EqVBjwspszySNrm2C5i4gQydC50XM0xkeKm9bGQ UZhNZSLoH7mBhFUlIA7wG4k2+o2XJR7e+9weQKqqZwHmM8d7+1tS3rK/VImZeL3tUOnA 5ofPfHoVwC+V2XEtFTyhxhrMVdGQTykyvweCpswhcBPCNTc7AhCfGIx8nXXQ2fBo/Dlp smbG7RP/J8cWRmdlmcFjAdz2YdvaFVMP9ZWsjkXmYWDzNXYlNQrjKhG1H5x3mmXqGyaS lQRc4rOLCAwaQMkb8NHE1ww2C+FETiwox39km1QbGmCEQAYkxjIPVVg7JM2dWDt7C67h 0Lwg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707301277; x=1707906077; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Ol9S0atgXTRMlRPwMU7mGPSjUu7OjdesfDZweTnFUmI=; b=ILqPJWp6Jj+lFykUtatiSv9O7JdnddxatP55yEWAzkKCjqRKypKo27wemUYOoKVtJZ d9pw/s5T7BaYtdAvQaqHPC44CW8Bk+j3jtKAc9qsNhfeT/eUrnqcbWt5d5U6O8fAOur9 MgLvKtK9ktBVA4Fce00yoHKtKZGb77JVizvYsq/bX9Dcbg9VC4xLGPOyMSeAr2gnYgMh SdRWIQItdBuyEyeHY+v5ontwIG7TXHArBQ00Yw8lxjhyS2uB+1SSYaUB/yyEJBFzHl82 drDKQY9sfuUmcothILALUsjiNwP/VEpgvFTHDLY/SaQNgljRZco4pJVw11kteG4AGgJe A8dg== X-Gm-Message-State: AOJu0YyldcKI7rDrDbCjeetms35DAxdqFxTkaFuzSiBsyidoOSINqrDJ FQzrEh8ayhqePTi7HsQFGDfd7cwTbLZAshDck5k4tjY4+w5JWKagFgjfAnhc26bq4ue/55rubYr qQdwVxVa4ZWgolr1y1nuf5+STN/rSCdURE5d20g== X-Google-Smtp-Source: AGHT+IGKFqSuJbj+R0th/JnYUq116IU2fZBNxWrTbRyYFcuUCooF4KZ9yPzb2NlU/emG9TILs0JJ7f/NS5DXswfWm8c= X-Received: by 2002:a4a:d2dd:0:b0:59a:38b4:1038 with SMTP id j29-20020a4ad2dd000000b0059a38b41038mr5375950oos.1.1707301276968; Wed, 07 Feb 2024 02:21:16 -0800 (PST) MIME-Version: 1.0 References: <742f202d-7990-4f51-b903-7a15e3fd33c2@app.fastmail.com> <3db4fbe0-22f4-44e2-a1a6-6cd85287df56@app.fastmail.com> In-Reply-To: <3db4fbe0-22f4-44e2-a1a6-6cd85287df56@app.fastmail.com> Date: Wed, 7 Feb 2024 11:21:05 +0100 Message-ID: To: Larry Garfield Cc: php internals Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Subject: Re: [PHP-DEV] Feature request: https://github.com/php/php-src/issues/13301 From: landers.robert@gmail.com (Robert Landers) On Tue, Feb 6, 2024 at 9:22=E2=80=AFPM Larry Garfield wrote: > > On Tue, Feb 6, 2024, at 7:56 PM, =D0=93=D1=80=D0=B8=D0=B3=D0=BE=D1=80=D0= =B8=D0=B9 Senior PHP / =D0=A0=D0=B0=D0=B7=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D1= =87=D0=B8=D0=BA Web wrote: > > Thanks Larry, I will read both articles next weekend. > > > > Am not even talking about changing `throw` to `raise`. > > > > Am talking only about: > > - production ready code > > - that should be able to refactor with error collectors (that was not > > implemented years ago) > > - without touching return types > > - without touching input arguments of existing code > > - without possible code fall after throw exception: you have to try/cat= ch > > all places you use that function (sometimes you predict possible error,= and > > yes, write return class/enum to extend/refactor it later) > > (and yes, if old code did not support returning null/null-object before= - > > you have to refactor return types then) > > > > While working with queues you have a list of tasks > > - then you reduce it to smaller with reducer (unique/filter/merge) > > - then do some queries > > - then walk initial data using reduced results: copying reports to save > > errors/warnings to each task separately > > > > It cannot be solved with exceptions. In addition, large arrays throw > > exceptions that cause timeloss. It's definitely not a tool for. > > Also your method could return many errors (today - only one > > error/exception), and you need to write a second method, then call the > > second method, then debug the second method. > > > > So what's in rest? Arrays collection of warnings and errors. Changing > > return types or passing second-return by reference. > > > > [ Enum case ~ DTO output ] covers newly written code. Old code is > > uncovered. You have to rewrite a full tree, that's why some trick is > > necessary. > > > > I did it my way with an error bag stack. I enable it inside the functio= n or > > in place I call the function. I want to share this experience, and imag= ined > > it would be better for all users. It could be done without 2 classes, 1= 0 > > functions and work with push/pop/current (closer to ob_start/ob_get_cle= an > > story). > > I guess it could be implemented if `raise` world will put any data to t= he > > current error bag in the stack. Exactly if the current error bag is pre= sent > > (declared manually like you can declare() strict types or ticks for som= e > > scope). > > > > I agree that there's more mandatory problems to solve that I didn't eve= n > > know about. > > I tried to talk about error handling with a few developers, all of them > > recommend: > > 1. Use exceptions, don't make anything fresh > > 2. Do validation at the script start to reduce the count of errors late= r > > > > I've just encountered cases where bugs come from within - once you > > integrate a really bad external system with its own checks, which are > > described in hundreds of documents, I'm sure you'll encounter new bugs = once > > the "working" code is released to production. And then you will need to > > quickly and easily reorganize it. > > > > And you can't. > > And you will be sad. > > And, "PHP moves differently" is a completely wrong principle, I believe= in > > "watching for". > I think there's a subtle but important difference here between what you'r= e describing as the problem and what you implied the solution was (which I = then ran with). > > What you're talking about is trying to change the error handling model of= existing code without changing function signatures. There are only two po= ssible ways to do that, both of them bad: Unchecked exceptions and globals. > > What I described, based on the syntax you offered, is checked exceptions,= which necessarily means changing the function signature. Error handling i= s part of the contract of a function. If its error handling changes, it *s= hould* have a signature change to indicate that. (That unchecked exception= s do not do that is the problem with unchecked exceptions.) So if "no chan= ges to existing code" is the goal, checked exceptions as I describe them ar= e not the answer you are looking for. > > It seems from your latest message that you're describing more a generaliz= ed version of `json_last_error()` and similar functions. The problem there= is that such an API design is generally considered very poor practice outs= ide of C, because it's all necessarily based on globals and "hope you remem= bered to check the thing that no one told you to check and is not even slig= htly obvious to check". That is not something I would want better support = for in the language at all. There's probably cleaner ways to emulate it in= user-space, but that is for a particular application to sort out. There's= definitely cleaner monadic solutions (which I've written before and are qu= ite neat) using a writer/logger monad, but that again doesn't meet your "do= n't change existing code" requirement. I don't think anything the language= can do will meet that requirement and be a good design. > > --Larry Garfield > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: https://www.php.net/unsub.php > Oh wow, this conversation got really interesting while I was asleep :D I think this could actually be interesting in a semi-backwards-compatible way, by just adding some syntax sugar: function getResult(): ?Result, ?ResultError { if($error) return null, $error; } instead of, but this would still work when destructuring: function getResult(): array { if($error) return [null, $error); } This would still work (the "backwards compatible" part): [$result, $error] =3D getResult(); or this: $result, $error =3D getResult(); Essentially, return types with a comma are just a "strongly typed array" and a comma on the left-hand side of assignment is just a destructure. Robert Landers Software Engineer Utrecht NL