# Any reason why backed enums are limited to int and string only?

- Thread: https://externals.io/message/113883
- Original archive: https://news-web.php.net/php.internals/113883
- Started by olleharstedt@gmail.com on 2021-03-31T18:44:19+00:00
- Messages: 4
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #113883 — olleharstedt@gmail.com — 2021-03-31T18:44:19+00:00

URL: https://externals.io/message/113883#113883

Hello,

I was trying to implement the result type from OCaml using enums, and
noticed that they can only carry int or string data, not arbitrary
data. Any specific reason for this?

Example:

    enum Result: mixed {
        case Ok = null;
        case Error = null;
    }

Error with:

Fatal error: Enum backing type must be int or string, mixed given in
/tmp/enum.php on line 3

More specifically, for this idiom to work, "Ok" needs to be mixed and
"Error" should be string.

OCaml idiom: https://ocaml.org/learn/tutorials/error_handling.html#Result-type

Olle

---

## #113884 — Rowan Tommins — 2021-03-31T18:51:19+00:00

URL: https://externals.io/message/113883#113884 · In reply to: #113883

>      enum Result: mixed {
>          case Ok = null;
>          case Error = null;
>      }


Sounds like what you're looking for is not "pure" enums, but Algebraic 
Data Types. Larry and Ilija have a grand plan for adding those, but 
there's a few steps to go yet: https://wiki.php.net/rfc/adts

The current enum values are just constants for simplifying serialization 
and lookup (translating to a code or database ID).

Regards,

-- 
Rowan Tommins
[IMSoP]

---

## #113885 — Ilija Tovilo — 2021-03-31T18:52:13+00:00

URL: https://externals.io/message/113883#113885 · In reply to: #113883

Hi Olle

> I was trying to implement the result type from OCaml using enums, and
> noticed that they can only carry int or string data, not arbitrary
> data. Any specific reason for this?
>
> Example:
>
>     enum Result: mixed {
>         case Ok = null;
>         case Error = null;
>     }
>
> Error with:
>
> Fatal error: Enum backing type must be int or string, mixed given in
> /tmp/enum.php on line 3
>
> More specifically, for this idiom to work, "Ok" needs to be mixed and
> "Error" should be string.
>
> OCaml idiom: https://ocaml.org/learn/tutorials/error_handling.html#Result-type

What you're trying to achieve here is not what backed enums are for.
Backed enums are for assigning each enum case a single, constant
representation that can be converted back and forth. As for Result,
what you'd want here is a dynamic, arbitrary value for both Ok and
Error, which is exactly what ADTs / tagged unions would allow you to
do:

https://wiki.php.net/rfc/tagged_unions

enum Result
{
    case Ok(mixed $result);
    case Error(mixed $error);
}

Optimally we'd define Result as "Result<TOk, TError>" but that's
another discussion :)

Note that work on ADT hasn't started yet.

Ilija

---

## #113886 — olleharstedt@gmail.com — 2021-03-31T19:17:08+00:00

URL: https://externals.io/message/113883#113886 · In reply to: #113885

2021-03-31 20:52 GMT+02:00, Ilija Tovilo <tovilo.ilija@gmail.com>:
> Hi Olle
>
> > […]
>
> What you're trying to achieve here is not what backed enums are for.
> Backed enums are for assigning each enum case a single, constant
> representation that can be converted back and forth. As for Result,
> what you'd want here is a dynamic, arbitrary value for both Ok and
> Error, which is exactly what ADTs / tagged unions would allow you to
> do:
>
> https://wiki.php.net/rfc/tagged_unions
>
> enum Result
> {
>     case Ok(mixed $result);
>     case Error(mixed $error);
> }
>
> Optimally we'd define Result as "Result<TOk, TError>" but that's
> another discussion :)
>
> Note that work on ADT hasn't started yet.
>
> Ilija
>
> --
> 
> To unsubscribe, visit: https://www.php.net/unsub.php
>
>

Got it, thanks guys!

Olle
