Re: Request for RFC Karma – kha ledalamxyz
| From: | Ben Ramsey | Date: | Sun, 29 Jun 2025 23:52:18 +0000 |
| Subject: | Re: Request for RFC Karma – kha ledalamxyz | ||
| References: | 1 2 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-127787@lists.php.net to get a copy of this message | ||
> On Jun 29, 2025, at 18:39, Thomas Nunninger <thomas@nunninger.info> wrote:
>
> Hi,
>
>> I’d like to request *RFC karma* to create a new proposal titled "*Fail-Fast
>> Assertion Operator (|=>!|)"*.
>> *About the Proposal:*
>> The idea is to introduce a new *fail_fast_assertion_operator* to PHP that provides a
>> concise and readable way to assert that an expression is valid and immediately fail otherwise.
>> *Example 1:*
>> $name = null;
>> $name =>! die("Missing name") > This is equivalent to:
>> if (!$name) {
>> die("Missing name");
>> }
>> *Example 2:*
>> $username =>! throw new Exception("Missing username");
>> $password =>! throw new Exception("Missing password");
>> is_valid($username, $password) =>! exit("Invalid credentials");
>> This is equivalent to:
>> if (!$username) {
>> throw new Exception("Missing username");
>> }
>> if (!$password) {
>> throw new Exception("Missing password");
>> }
>> if (!is_valid($username, $password)) {
>> exit("Invalid credentials");
>> }
>> The goal is to improve readability and reduce boilerplate in fail-fast validation
>> scenarios. This is particularly useful in input validation, functional pipelines, and early guard
>> clauses.
>> I’d like to open the discussion and collaborate with the community to refine and assess
>> its value and implementation feasibility.
>> Please let me know if anything else is required from my side.
>
>
> That seems to work today (https://3v4l.org/14aPa):
>
>
> try {
> $name ?? throw new Exception("variable is null or not defined");
> } catch (Exception $e) {
> echo $e->getMessage() . "\n";
> }
>
> try {
> $name = '';
> $name ?: throw new Exception("variable is falsy");
> } catch (Exception $e) {
> echo $e->getMessage() . "\n";
> }
>
>
> Regards,
>
> Thomas
You can also use logical OR (||), similar to other languages:
$name || throw new Exception("variable is falsy");
Cheers,
Ben
Attachment: [application/pgp-signature] Message signed with OpenPGP signature.asc
Attachment: [application/pgp-signature] Message signed with OpenPGP signature.asc