Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:112843 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 75710 invoked from network); 11 Jan 2021 20:50:13 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 11 Jan 2021 20:50:13 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 9ADC41804E4 for ; Mon, 11 Jan 2021 12:28:00 -0800 (PST) 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,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from mail-il1-f180.google.com (mail-il1-f180.google.com [209.85.166.180]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Mon, 11 Jan 2021 12:27:59 -0800 (PST) Received: by mail-il1-f180.google.com with SMTP id y13so464155ilm.12 for ; Mon, 11 Jan 2021 12:27:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=benramsey.com; s=google; h=from:message-id:mime-version:subject:date:in-reply-to:cc:to :references; bh=mk/AOej9NWVuxrx8U1YDsm1PlTFOQdJMeT+oA8MsOKU=; b=XoEq1GoSVTxLG1WqLFIwenqFIqS5HP7KWzryiiabsaLmszBS7hns+9K+RGqvkSB89Z 2Pcpmo3aLO87ohVPn5fZTisZ541XfDgj0eHZhm9LcY3YBMpMJdYs0bgGYhe/m+p70iDP xv8YyyVWrU5XzrVXvwNT0b0LNqkduDgLPWSoryK1p9xiv7LELn745Vsql+aqFpQtC4+4 zwQU86D3M/SObP5G5dgltErFg3ecXf0yNhnOFFbPU53m0kln84+Pl8hLinwNEkD09na6 zB6Bmu2IdMnrSFJ/RmDVL0miea/ol3m9Mm8t3TUIOPIZnLaM/T9U40v36lVpyqGSTT4M ezcQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:message-id:mime-version:subject:date :in-reply-to:cc:to:references; bh=mk/AOej9NWVuxrx8U1YDsm1PlTFOQdJMeT+oA8MsOKU=; b=ickypb6HT0yTtT6IcklKZv66Fp/OWmFIBFacrmGNMrBfOv1rqUYEQY2Ge4B5NLvBjD +5/YUMtdBF9NrlgFh2ZBLSiX3otEMIu/9alXSC0EJD6Ob9g2VXv1qeQarZdILN+6cQ/Q 72br3KIYGUoZXhHeD1nJ+ApR8T4qSQwtUx8y7QiHjzsvlhxHg//Xnne9+sN1nzYWhODF olfzVj1h6NlbSkMGz4KypZdiLu9vXsZVNptokxGEJEXvM7NN7O39VHyS+Th7iEU6GRAb 5CTDuFCKoirWSM4bl0OCjtKSHPRjhtgcR6AGrv5JDjcpPfuDQ31tDmLTVvHlc10dDtYV W6gQ== X-Gm-Message-State: AOAM531H+ONxEmQK2L6knEcU/WX6dzjY4Nvo1mJ4GRsD8ZayuQKRpgN4 v2T/kGyUnw5Zpo8gW8L7QzDt/Q== X-Google-Smtp-Source: ABdhPJy3OES7tLugGJoPmgFprTa2IOS5kuzuhRvTpFXGbv58yhI1KpGvnQcrZ2/qAYuBRnvOwF5YRA== X-Received: by 2002:a05:6e02:68b:: with SMTP id o11mr827134ils.237.1610396875742; Mon, 11 Jan 2021 12:27:55 -0800 (PST) Received: from [10.10.42.56] ([96.61.105.82]) by smtp.gmail.com with ESMTPSA id j65sm481040ilg.53.2021.01.11.12.27.54 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 11 Jan 2021 12:27:55 -0800 (PST) Message-ID: <490416F4-7E2F-454F-A317-43212CA6C2D1@benramsey.com> Content-Type: multipart/signed; boundary="Apple-Mail=_49BC4A53-2A7A-4CBC-8DC2-A697D239AFB0"; protocol="application/pgp-signature"; micalg=pgp-sha256 Mime-Version: 1.0 (Mac OS X Mail 14.0 \(3654.40.0.2.32\)) Date: Mon, 11 Jan 2021 14:27:54 -0600 In-Reply-To: Cc: php internals To: Larry Garfield References: X-Mailer: Apple Mail (2.3654.40.0.2.32) Subject: Re: [PHP-DEV] "TryX" idom for Enumerations From: ben@benramsey.com (Ben Ramsey) --Apple-Mail=_49BC4A53-2A7A-4CBC-8DC2-A697D239AFB0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 > On Jan 10, 2021, at 20:09, Larry Garfield = wrote: >=20 > On Sun, Jan 10, 2021, at 4:40 PM, Mark Randall wrote: >> On 10/01/2021 21:27, Larry Garfield wrote: >>> The "a method that begins with try is nullable, so watch out" idiom = is present in C# and Rust, but to my knowledge has never existed in PHP. = That doesn't make it bad; it actually combines quite well with the null = coalesce operator to allow for default values, making a valueOrDefault() = method unnecessary. >>=20 >> I get the advantages of returning null, in particular WRT the null >> coalescing operator. >>=20 >> However, when I see 'try' I instictively think 'exceptions' i.e. >> try/catch when the reality is it's the opposite and it would be the >> non-try functions which throw. >>=20 >> But it makes sense for those to throw. >=20 > That was my initial response as well, and my main concern. To a PHP = developer, the terminology seems backwards. But it's not like we = haven't adopted idioms and syntax from other languages before. >=20 >> However if you will permit me a tangent... >>=20 >> There is an alternative, of sorts. Something I tried investigating = when >> I first started looking into PHP-SRC, but lack the skill and = knowledge >> to implement myself. >>=20 >> A shorthand try / catch of something like the form: >>=20 >> ( ResultExpr, ...) >>=20 >> At which point try=46rom becomes: >>=20 >> $order =3D attempt(SortOrder::tryFrom($input), ValueError =3D> = SortOrder::Asc); >>=20 >> Which would be the equivilent of: >>=20 >> function attempt(callable $try, array $map): mixed { >> try { >> return $try(); >> } >> catch ($e) { >> foreach ($map as $class =3D> $expr) { >> if (is_subclass_of($e, $class, true)) { >> return $expr(); >> } >> } >>=20 >> throw $e; >> } >> } >>=20 >> Or just allow a mixed value to be given without a mapping for = catching >> Throwable. >>=20 >> If added as a language construct, except each expr and $try itself = would >> be parsed closure at language construct level to avoid needing to = fn() >> them all e.g. >>=20 >> $foo =3D attempt(fn() =3D> SortOrder::tryFrom($input), [ ValueError = =3D> fn() >> =3D> null ]); >>=20 >> Just a thought, perhaps a cleaner solution to a wider problem. A try = / >> catch combined with a match. >>=20 >> It avoids the need for two methods, just provide the one that throws = and >> use shorthand to assign a null if it throws. >>=20 >> Mark Randall >=20 > The main issue I see is that creating an exception is quite expensive. = (Or it was the last time I bothered benchmarking it, which admittedly = has been a while.) So even if you had some built in "catch and match" = pseudo-function, you're still paying the cost of the exception, which is = not cheap. >=20 > catch-and-match is an interesting idiom, but as you say it's somewhat = tangential to the question at hand. >=20 > --Larry Garfield I have no problem introducing this idiom (or similar). In fact, I = welcome it. In userland, the same concept is often achieved with methods like = `fromOrNull()`, and as Mark points out, the word =E2=80=9Ctry=E2=80=9D = makes me think the method should throw an exception if it fails. I=E2=80=99= m not advocating for `xOrNull()`, though, since I think that smacks of = Hungarian notation, but maybe we can come up with something that is a = more PHP-ish name. ;-) =E2=80=9CThere are two hard things in computer science: cache = invalidation, naming things, and off-by-one errors.=E2=80=9D --Phil = Karlton Cheers, Ben --Apple-Mail=_49BC4A53-2A7A-4CBC-8DC2-A697D239AFB0 Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename=signature.asc Content-Type: application/pgp-signature; name=signature.asc Content-Description: Message signed with OpenPGP -----BEGIN PGP SIGNATURE----- iHUEAREIAB0WIQToXQMR3fpbrPOmEOewLZeYnIwHGwUCX/y0ygAKCRCwLZeYnIwH G1T/AP43WKUohPHIfve4zf2Z44fjMiH1HmWgA20Du3cs5clTLwD8DVJ6mHv/O+GT FUvWJUJxhUQ5VF1TglBVsv5yTY0BzfM= =EAja -----END PGP SIGNATURE----- --Apple-Mail=_49BC4A53-2A7A-4CBC-8DC2-A697D239AFB0--