Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:121474 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 71907 invoked from network); 26 Oct 2023 17:31:44 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 26 Oct 2023 17:31:44 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id EDB01180504 for ; Thu, 26 Oct 2023 10:31:42 -0700 (PDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-2.6 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_LOW, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS34011 80.237.132.0/24 X-Spam-Virus: No X-Envelope-From: Received: from wp160.webpack.hosteurope.de (wp160.webpack.hosteurope.de [80.237.132.167]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Thu, 26 Oct 2023 10:31:42 -0700 (PDT) Received: from [2a02:8109:b323:1700:e5f9:7932:39d4:19d7]; authenticated by wp160.webpack.hosteurope.de running ExIM with esmtpsa (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) id 1qw4Cq-0003OV-Fp; Thu, 26 Oct 2023 19:31:40 +0200 Message-ID: Date: Thu, 26 Oct 2023 19:31:40 +0200 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Content-Language: en-US To: internals@lists.php.net References: In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-bounce-key: webpack.hosteurope.de;marc@mabe.berlin;1698341502;1b33fe6c; X-HE-SMSGID: 1qw4Cq-0003OV-Fp Subject: Re: [PHP-DEV] Basic Type Alias From: marc@mabe.berlin (Marc) Hi, On 26.10.23 08:37, 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 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? > I would love to see type aliases added to PHP but I think it would be very less useful if it's scoped per file only. Like `type UserId = string;` in it's self already improves readability but only if reusable by different files and because of that a big question about autoloading/performance comes in here as well. About the syntax: I think this should be consistent with how we declare types right now ... `type UserId: string;` instead of `type UserId = string;` Another question is how the type annotations will behave if the aliased type is hinted: type UserId: string; function test(UserId $userId): string {     return $userId; } test("its-me"); Also, would it be possible to handle types as specialized classes including namespaces, helper functions and usable with reflection? type UserId: string; type OtherId: UserId; UserId::base // "string" OtherId::base // "string" UserId::nullable // false UserId::isValid(mixed $var): bool $refl = ReflectionClass(UserId::class); $refl = ReflectionType::from(UserId::class); ... Best, Marc