$fontWeight = match ($weight) { 100 => "Super Thin", 300 => "Thin", 400 => "Normal", 600 => "Bold", 900 => "Heavy" };
Error prone code... What's the value of $fontWeight if $weight = 101 ? Is there an error ?
Safe "Switch expressions" constructs should assert there is always a default clause (i'm looking at you rust 😍)
match has a default case too. Also, you can comma-separate multiple values:
match
$fontWeight = match ($weight) { 100, 200 => "Super Thin", 300 => "Thin", 400, 500 => "Normal", 600, 700, 800 => "Bold", 900 => "Heavy", default => "Not valid" };
Edited the example in the article accordingly.
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
$fontWeight = match ($weight) {
100 => "Super Thin",
300 => "Thin",
400 => "Normal",
600 => "Bold",
900 => "Heavy"
};
Error prone code... What's the value of $fontWeight if $weight = 101 ? Is there an error ?
Safe "Switch expressions" constructs should assert there is always a default clause (i'm looking at you rust 😍)
match
has a default case too. Also, you can comma-separate multiple values:Edited the example in the article accordingly.