Re: Basic Type Alias
| From: | Robert Landers | Date: | Thu, 26 Oct 2023 10:46:08 +0000 |
| Subject: | Re: Basic Type Alias | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-121467@lists.php.net to get a copy of this message | ||
On Thu, Oct 26, 2023 at 8:37 AM Oladoyinbo Vincent
<oladoyinbov@gmail.com> 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 the
type or
> typealias keyword,
> 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); // TypeError
>
> ```
>
> Since 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?
Please make these aliases either per-file (easiest to implement with
the current design of types, in my humble opinion) or able to be
namespaced.
If I have an alias called Currency and a class somewhere else called
Currency, what will happen if one file is included before the other?
How will I specify that I want an alias or class of the same name?
Will I need to alias the class, or can I alias the type alias?
How will autoloading work with composer? Will a file called
"Currency.php" be needed to discover the type alias?
Robert Landers
Software Engineer
Utrecht NL