44 ms·
The example in the article is poorly chosen, because an associative array (map) would be more suitable. What you wrote is almost the same with PHP: ['abc'
by idoubtit 5y ago
The example in the article is poorly chosen, because an associative array (map) would be more suitable. What you wrote is almost the same with PHP:
['abc' => 2, 'def' => 3][$x] ?? 'default';
PHP's `match` is a kind of functional `switch`. For instance, in the following example, an associative array would induce several useless function calls, and `match` is more readable than a sequence of if+returns:
return match ($key) {
"id", "ID" => unique_id(),
"UUID" => generate_uuid(),
default => findExternalId($key),
};