# Literal types RFC

- Thread: https://externals.io/message/111996
- Original archive: https://news-web.php.net/php.internals/111996
- Started by Bogdan Ungureanu on 2020-10-04T23:38:01+00:00
- Messages: 3
- 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

---

## #111996 — Bogdan Ungureanu — 2020-10-04T23:38:01+00:00

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

Hello internals,

Since union types have landed in PHP, this allows us the possibility to
have literal types support as well and I would like to hear your
thoughts about an RFC for it.

I think a feature like this would greatly improve the way we model our code
in very common use cases. For example, let's consider having a function
that returns the status of a post.

```php
/**
/** @return "draft"|"published"
*/
function getStatus(): string { }
```

Would become

```php
function getStatus(): "draft" | "published" { }
```

Of course this could become quite messy if there are more than a few
literal types, in which case separate RFCs could be created for type
aliases and valueof/keyof features.

I would also like to mention that I also worked on a basic implementation
for the first phase of the implementation; it's nowhere near to be
completed, but I think I can finish it with some guidance/help from you.

Regards,
Bogdan Ungureanu

---

## #111997 — Max Semenik — 2020-10-04T23:55:48+00:00

URL: https://externals.io/message/111996#111997 · In reply to: #111996

пн, 5 окт. 2020 г., 02:38 Bogdan Ungureanu <bogdanungureanu21@gmail.com>:

>
> function getStatus(): "draft" | "published" { }
>

It sounds that what you're really asking for is enums, e.g.

enum Status {
    Draft,
    Published
};

function getStatus(): Status { ... }

---

## #111998 — Bogdan Ungureanu — 2020-10-05T00:03:14+00:00

URL: https://externals.io/message/111996#111998 · In reply to: #111997

Yeah, kind of, the problem with enums from what I understood from the
current RFCs is that you'll need extra code to map a scalar to that
specific enum value.

Besides that, a literal type implementation is afaik simpler to
implement/spec than enums, thus having a better chance to pass voting.



> пн, 5 окт. 2020 г., 02:38 Bogdan Ungureanu <bogdanungureanu21@gmail.com>:
>
> > […]
>
> It sounds that what you're really asking for is enums, e.g.
>
> enum Status {
>     Draft,
>     Published
> };
>
> function getStatus(): Status { ... }
