Re: [PHP-DEV] PHP 8.2 proposal: "match", allow "default" as conditional_expression, e.g. 'en_US', 'en_GB', default => loadDefaults()

2021-11-22 Thread Andreas

On 22.11.21 22:06, Nikita Popov wrote:

On Mon, Nov 22, 2021 at 7:18 PM Andreas  wrote:

Hi internals,

This is a proposal to allow to append the `default` pattern by
comma to
the end of the last match branch. (Like a conditional_expression)

This allows to re-use the return_expression if required and avoids
code
duplication.

PROPOSAL: PHP 8.2

 loadGermanLanguageSettings(),
 'en_US', 'en_GB', default => loadDefaultLanguageSettings(),
};
?>


Isn't this equivalent to just this?

 loadGermanLanguageSettings(),
 default => loadDefaultLanguageSettings(),
};

'en_US' and 'en_GB' will already go to the default branch if they're
not listed explicitly.

Regards,
Nikita


Hi Nikita,

nice to meet you. Yes, that's indeed possible.

But I personally like to list all handled conditional_expressions (here
the $locales) in the match
in order to directly see in the code for later reference, which of them
are explicit handled (and which are handled by the default pattern).

This proposal is mainly syntactic sugar to avoid code duplication, in
this case the duplicate return_expression loadDefaultLanguageSettings().

Just as a side note: PHPStorm's inspection suggests "Merge with default
arm" every time because of this code duplication.

Regards,
Andreas



Re: [PHP-DEV] PHP 8.2 proposal: "match", allow "default" as conditional_expression, e.g. 'en_US', 'en_GB', default => loadDefaults()

2021-11-22 Thread Nikita Popov
On Mon, Nov 22, 2021 at 7:18 PM Andreas  wrote:

> Hi internals,
>
> This is a proposal to allow to append the `default` pattern by comma to
> the end of the last match branch. (Like a conditional_expression)
>
> This allows to re-use the return_expression if required and avoids code
> duplication.
>
> PROPOSAL: PHP 8.2
>
>  return match ($locale) {
>  'de_DE', 'de_CH', 'de_AT' => loadGermanLanguageSettings(),
>  'en_US', 'en_GB', default => loadDefaultLanguageSettings(),
> };
> ?>
>

Isn't this equivalent to just this?

 loadGermanLanguageSettings(),
 default => loadDefaultLanguageSettings(),
};

'en_US' and 'en_GB' will already go to the default branch if they're not
listed explicitly.

Regards,
Nikita


[PHP-DEV] PHP 8.2 proposal: "match", allow "default" as conditional_expression, e.g. 'en_US', 'en_GB', default => loadDefaults()

2021-11-22 Thread Andreas

Hi internals,

This is a proposal to allow to append the `default` pattern by comma to
the end of the last match branch. (Like a conditional_expression)

This allows to re-use the return_expression if required and avoids code
duplication.

PROPOSAL: PHP 8.2

 loadGermanLanguageSettings(),
    'en_US', 'en_GB', default => loadDefaultLanguageSettings(),
};
?>

// PHP 8.1 (Current implementation works like before)

 loadGermanLanguageSettings(),
    'en_US', 'en_GB' => loadDefaultLanguageSettings(),
    default => loadDefaultLanguageSettings(),
};
?>

The `default` pattern must be the last item in the last expression branch.

Thanks,
Andreas

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: https://www.php.net/unsub.php