Literal types RFC

php.internals

Bogdan Ungureanu

5 years ago
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

Max Semenik

5 years ago
пн, 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 { ... }

Bogdan Ungureanu

5 years ago
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. On Mon, Oct 5, 2020 at 2:56 AM Max Semenik <maxsem.wiki@gmail.com> wrote: