On 26/11/2024 18:10, Volodymyr Volynets wrote:
> I have an idea which will save a lot of code. I am proposing to add":
>
> return when [condition], [return value];
>
> This construct will remove a lot of ifs statements after method calls.
> For example:
>
> $result = Class->method();
> if (!$result['success']) {
>      return $result;
> }
>
> This becomes:
> return when !$result['success'], $result;
>
> Any thoughts?

Is there a language that uses a similar syntax? I don't find it readable. It also uses a new keyword (when) when you can reuse if I'd say, also comma does not look like a good separator here.

Some alternatives could be:

Perl&Ruby-like postfix if:

    return $result if (!$result['success']);

or making return an expression that will allow us to do like this

    $result['success'] ?: return false;

and

    match ($something) {
        123 => do_stuff(),
        default => return false,
    }

but they both can open an endless can of worms I'd say so I agree with Christoph that the current syntax is good enough

Anton

Reply via email to