Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:121475 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 78279 invoked from network); 26 Oct 2023 19:23:45 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 26 Oct 2023 19:23:45 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 58F55180543 for ; Thu, 26 Oct 2023 12:23:44 -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.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H5,RCVD_IN_MSPIKE_WL,SPF_HELO_PASS,SPF_NONE, T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS29838 64.147.123.0/24 X-Spam-Virus: No X-Envelope-From: Received: from wout1-smtp.messagingengine.com (wout1-smtp.messagingengine.com [64.147.123.24]) (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 12:23:43 -0700 (PDT) Received: from compute1.internal (compute1.nyi.internal [10.202.2.41]) by mailout.west.internal (Postfix) with ESMTP id 4EBB73200A7A for ; Thu, 26 Oct 2023 15:23:42 -0400 (EDT) Received: from imap50 ([10.202.2.100]) by compute1.internal (MEProxy); Thu, 26 Oct 2023 15:23:42 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= garfieldtech.com; h=cc:content-type:content-type:date:date:from :from:in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:sender:subject:subject:to:to; s=fm3; t=1698348221; x= 1698434621; bh=G2tvBsPtbZfdvHbw5hGuZg/DrNocEmZtiF8rwFVo4C8=; b=n qj1UsMhvbCaOjjobu5slsGpHca5wm64IV2TpfH9DgaiQyxcuVxtwgeTovt6sf0oI HuPaEsfLQRbdGLSnIdFQd5T3cL17wYdf1gCsK6cfDxlQWAe0w18Ykjkxd6UYWj/0 BNtQ4bgJYQfXmF/bUwsPr1chzcfGv13MNmcHTsgCaW1hOmWOe4AOjltV7/d+dsmw DlZPxUGv4aZ9w8tS/clVB8gCNsSlRQv4iEM3twYdgdl0t75nK6xNlQkYTflWBPkc BwFMAhv77YYlDb9LOHufcYXvWtcmHQ4gAfsd3NFnQj3GEDg7stlyEXCmZfFS5pWw r10PIyGmrzwc7XWRRizgg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:sender:subject :subject:to:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm3; t=1698348221; x=1698434621; bh=G2tvBsPtbZfdv Hbw5hGuZg/DrNocEmZtiF8rwFVo4C8=; b=MaBEE0AX76EkpdOSPn7LJEdxXTrsW G93Z2oTm75wkcvZzEYIu6qnxkbSUNW3ouiEZg8FggCNkC8qJBWADNU9nGAZzg7qp Kn8IngHGkVfErfzSqmZjQAFKFpv8OnArnkwDBYeLnLNZOCL9aF2+lpg64XbBoaWi vA82fejAB4pdgzZeA6a2odHBsQppEQH6p4wCOSc+42DiLbZEOJYp/Z26a1+kkrF0 eHll74ElKUlgWTpVKwkKeH38tVEEp5sr0X614Z/hb2t8ZD7EaEpAvC6NIrA6ioKb S073Mq9wvubbc2C+pxDp4NGB1kN0+nqRIGB1csP6AkcfpFuDdwhYh5CIA== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvkedrledvgddufeeiucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepofgfggfkjghffffhvffutgesthdtredtreertdenucfhrhhomhepfdfnrghr rhihucfirghrfhhivghlugdfuceolhgrrhhrhiesghgrrhhfihgvlhguthgvtghhrdgtoh hmqeenucggtffrrghtthgvrhhnpeeglefgkeduiedvvdetffeujefftdfhjeeiveehgfff keduveektddvledvvdfffeenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmh grihhlfhhrohhmpehlrghrrhihsehgrghrfhhivghlughtvggthhdrtghomh X-ME-Proxy: Feedback-ID: i8414410d:Fastmail Received: by mailuser.nyi.internal (Postfix, from userid 501) id 6EF981700089; Thu, 26 Oct 2023 15:23:41 -0400 (EDT) X-Mailer: MessagingEngine.com Webmail Interface User-Agent: Cyrus-JMAP/3.9.0-alpha0-1048-g9229b632c5-fm-20231019.001-g9229b632 MIME-Version: 1.0 Message-ID: In-Reply-To: References: Date: Thu, 26 Oct 2023 19:23:18 +0000 To: "php internals" Content-Type: text/plain Subject: Re: [PHP-DEV] Basic Type Alias From: larry@garfieldtech.com ("Larry Garfield") On Thu, Oct 26, 2023, at 6:37 AM, 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]; What would this one even do? Type aliases have been discussed several times in the past, and a lot of people are strongly in favor, myself included. They are arguably a prerequisite for callable types. (Or rather, the people who want to work on callable types refuse to until we get type aliases. :-) ) The complication is, as some other replies noted, their scope. File local type aliases would be pretty easy: ``` use type int|float as numeric; function add(numeric $a, numeric $b): numeric {} ``` However, that's also not all that useful. A library that uses some complex type (such as a callable type) could not provide a reusable and standardized alias for it, which means everyone would have to re-declare it themselves in every file that uses it, and would probably all use different names (numeric, number, num, etc.), making it all very confusing. App-wide aliases run into the autoloading problem. If the engine runs across the add() function above, and "numeric" isn't defined, what can it do? Currently, all it can do is trigger autoloading, which only works for class-ish constructs (class, interface, trait, enum). Making type aliases a full class-like construct seems... weird, and over-engineered. But if not, how do we autoload them? And if they do autoload somehow, does that mean we end up with a bunch of files with a single `type` line in them? That seems not-good. You also then have to ask how they interact with namespaces. So far no one seems to like my idea of "composer has a files block, PHP has a preloader, they're small, so who the hell cares about autoloading, just greedily load them and move on with life", even though I think that's the best answer. :-) So any type alias proposal would need to sort out the above "definition problem" in a way that's generally acceptable. That's been the hold up for 3 years, I think. I'm also not sure why you're mentioning generics at all. Type aliases would be helpful with generics, but they're also helpful without generics. They're orthogonal and not especially related. I agree that there's no reason to wait for generics to add type aliases. --Larry Garfield