Hello,
I would like to get RFC Karma for my wiki account. My username is:
jkroon.
The proposed RFC will introduce Enums with Associated Values for cases
and is inspired by Rust and Swift.For example it would allow for the following:
<?php enum ApiResult { case Success(string $data, int $statusCode); case Failure(Throwable $error, ?int $statusCode); } function handle(ApiResult $r): string { return match ($r) { ApiResult::Success => 'OK ' . $r->statusCode . PHP_EOL, ApiResult::Failure => 'NO ' . ($r->statusCode ?? 'unknown') . PHP_EOL, }; } echo handle(ApiResult::Success('All good', 200)); echo handle(ApiResult::Failure(new Exception('Bad request'), 400)); echo handle(ApiResult::Failure(new Exception('Server error'), null));Regards,
Jordi
Please be aware that there are already plans for this feature (RFC is a bit dated for now so don't take literally):
https://wiki.php.net/rfc/tagged_unions
Which is currently blocked pending pattern matching, currently in discussion:
https://wiki.php.net/rfc/pattern-matching
I'm regularly around the phpc.chat Discord if you want to talk about it more directly. Right now the main blocker is time availability, and getting pattern matching done. (See it's thread for the currently open questions.)
--Larry Garfield