Re: Basic Type Alias
| From: | Lanre Waju | Date: | Thu, 26 Oct 2023 16:24:07 +0000 |
| Subject: | Re: Basic Type Alias | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-121471@lists.php.net to get a copy of this message | ||
I would like to voice my support for this and i'm willing to help with the implementation. As for differentiating between type aliases and classnames, only one of them can be instantiated.
Lanre
On 2023-10-26 12:37 a.m., Oladoyinbo Vincent wrote:
Greetings to you all, I will like to submit an RFC on introducing type alias to php. Even though Generics won't or never be introduced to PHP, personally i will like php to add the type alias feature. Type aliases provide a mechanism to create more descriptive and readable type hints in PHP code. This enhancement will improve code readability and maintainability and also to promote type reusability. Motivation: The motivation behind introducing type aliases is to: 1. Enhance code readability by allowing developers to use meaningful type hint names. 2. Improve code maintainability by reducing the likelihood of type hint updates throughout the codebase. 3. Encourage the adoption of best practices for type hinting. Proposal: Syntax: Type aliases will be declared using thetypeortypealiaskeyword, followed by the alias name, an equal sign (=), and the type it is aliased to. Sample:type MyType = string; // or typealias MyType = string; // Advance type MyType = string|null; // or type MyType = [string, null];Usage: Type aliases can be used in parameter and return type hints as follows:function greetings(MyType $message): string { // Implementation } greetings(1); // TypeErrorSince this is just a basic feature, Type aliases Inheritance (compound type) may not be added, since we are not digging deeper into generics :). References: https://docs.python.org/3/glossary.html#term-type-alias https://doc.rust-lang.org/beta/reference/items/type-aliases.html https://www.typescriptlang.org/docs/handbook/typescript-in-5-minutes-func.html#type-aliases So, What do you think?