Re: Request for RFC Karma – khaledalamxy z
| From: | Khaled Alam | Date: | Mon, 30 Jun 2025 14:52:36 +0000 |
| Subject: | Re: Request for RFC Karma – khaledalamxy z | ||
| References: | 1 2 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-127800@lists.php.net to get a copy of this message | ||
Hi Ilija, Ben, and Thomas,
Thanks for the rapid feedback. You’re right that PHP’s existing
expression-throw idioms cover some use cases, but
=>! fills three gaps:
1. Comprehensive “falsy” check *=>!* consistently treats *all* PHP-falsy
values (null, false, 0, "", []) as failure, in a single, unambiguous
operator.
// Existing: must choose between ??, ?:, ||, each with its own
quirks$name ?? throw… // only null$name ?: throw… //
also empty strings and "0"$name || throw… // *can’t embed in
function call*
// With =>!, you get exactly “fail on any falsy”:$name =>! throw new
Exception("Missing name");
2. Value forwarding
All of the above patterns discard or ignore the original value when used
inline. With *=>!*, the *guarded value* is both asserted and *returned*, so
you can write:
// Inline validation + use in one expressionsendEmail( getUserEmail()
=>! throw new Exception("Email required") );
This both throws on a missing/falsy email and — when valid — passes the
exact email string through to sendEmail().
3. Clean, declarative syntax
Because it’s a dedicated operator rather than a hacky boolean trick, =>!:
-
Reads as *“take X, or fail”* in one glance.
-
Avoids parentheses gymnastics or confusing operator precedence.
-
Encourages fluent, lets you chain guards:
$userId = $input["id"] =>! throw new Exception("ID missing")
=>! isValidId(...) =>! throw new Exception("Invalid ID");
Demonstration:
[image: image.png]
This was primarily an exercise to familiarize myself with the internals
process. I’d love to gather feedback - and some RFC-karma - before drafting
a full proposal on the mailing list.
Thanks again for your time and guidance!
Regards,
Khaled
On Mon, Jun 30, 2025 at 5:43 PM Ilija Tovilo <tovilo.ilija@gmail.com> wrote:
> Hi Khaled
>
> On Mon, Jun 30, 2025 at 1:03 AM Khaled Alam <khaledalam.net@gmail..com>
> wrote:
> >
> > I’d like to request RFC karma to create a new proposal titled "Fail-Fast
> Assertion Operator (=>!)".
> >
> > $name =>! die("Missing name");
> > This is equivalent to:
> > if (!$name) {
> > die("Missing name");
> > }
>
> As mentioned by the other responses, given that throw is an expression
> you can already achieve this through various operators:
>
> // Throw is $name is null
> $name ?? throw new Exception();
> // Throw if $name is empty
> $name ?: throw new Exception();
> // Throw if $name is falsy
> $name || throw new Exception();
> $name or throw new Exception();
> // Throw if $name is truthy
> $name && throw new Exception();
> $name and throw new Exception();
>
> It's not clear in what way your proposal is different. Hence, I'm
> pausing the granting of RFC karma until these details are clarified.
>
> Cheers
>