Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:121530 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 13021 invoked from network); 30 Oct 2023 15:42:44 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 30 Oct 2023 15:42:44 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id AE9BD180511 for ; Mon, 30 Oct 2023 08:42:43 -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 wout3-smtp.messagingengine.com (wout3-smtp.messagingengine.com [64.147.123.19]) (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 ; Mon, 30 Oct 2023 08:42:43 -0700 (PDT) Received: from compute1.internal (compute1.nyi.internal [10.202.2.41]) by mailout.west.internal (Postfix) with ESMTP id 6FA2A320029B; Mon, 30 Oct 2023 11:42:41 -0400 (EDT) Received: from imap50 ([10.202.2.100]) by compute1.internal (MEProxy); Mon, 30 Oct 2023 11:42:41 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= garfieldtech.com; h=cc: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= 1698680560; x=1698766960; bh=suCR5mf9qqWzrmL1pJxZSodv+V/LTJuvWRA mXClu7Is=; b=CV5uFctMDar7rcTkf5qpNFjzY/bnqwfQVyIFrjp182j643ulkAz FXJUIZC+CI8usD3AqmrtIQJCIOqGVZ1V+1g/CamtfMS/ncwg8A3ukpZEfGCOHFu6 65sRzshaTZP9JSicyCi9oWkNu+STBe6y1GLrnbC4jPUI8Ez4UvQy9ZSq2vTEarnu 3O1Jd80HLaJWPTwq9HQcNfZ+F5ZHo0Fuv3tb/YAURG5XtBWG0OFWF0ClZ9gJlw1G iKHj8DAHvf1edmeoV8o5AEAREB418m6gS7s688GRk83LyczH2GCYEhn7ii78vqQv vxdYTHEgq34plRH4uGE3Wh0FNSTfEJEBXtg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc: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=1698680560; x=1698766960; bh=suCR5mf9qqWzr mL1pJxZSodv+V/LTJuvWRAmXClu7Is=; b=AfjvDNSkuVUzOh7HhoBfqBc3xnuTu jS7NzXKXgMfu3WgBuNiqwksVkBTna7HLJxkEY+QIgP2ERXLz4Tm1IOjLqQfaPP6p 5rCZIf5PsnN3D7AJWTIXaGp/DIvWddA9ivrdDB9nOQzpyR9gLvcUR/QqRklBI1y5 k+Kgp3Y+ivTyQaDI1gVIP/JQu/dg+L3HMYqn1UrBZVukyl0kylz5/BS9+sAEGrb6 Nc+flcHtP6YgxPzOuQSpJ1D4/GwQAQZpTc4OPyNTA+3CeDmDkEcfa77TUjN0tIcp bxvABBhjY8Lk60TudXXa8aEfGLXfZgJ3alR1p9LQFPNz74Xwl+br+iWaw== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvkedruddttddgjeekucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepofgfggfkjghffffhvfevufgtsehttdertderredtnecuhfhrohhmpedfnfgr rhhrhicuifgrrhhfihgvlhgufdcuoehlrghrrhihsehgrghrfhhivghlughtvggthhdrtg homheqnecuggftrfgrthhtvghrnhepueeludelieekhfekkefgieduffejffffheevkedu ueegtedtfeetheekgeeutdfgnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpe hmrghilhhfrhhomheplhgrrhhrhiesghgrrhhfihgvlhguthgvtghhrdgtohhm X-ME-Proxy: Feedback-ID: i8414410d:Fastmail Received: by mailuser.nyi.internal (Postfix, from userid 501) id B04E61700089; Mon, 30 Oct 2023 11:42:40 -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: <10e7aed0-00b8-4140-a1e1-bb0a33e90938@app.fastmail.com> In-Reply-To: References: Date: Mon, 30 Oct 2023 15:42:19 +0000 To: "Jorg Sowa" , "Oladoyinbo Vincent" Cc: "php internals" Content-Type: text/plain Subject: Re: [PHP-DEV] Basic Type Alias From: larry@garfieldtech.com ("Larry Garfield") On Sun, Oct 29, 2023, at 9:52 PM, Jorg Sowa wrote: > I really like the idea and I would love to see it in PHP. I'm wondering > however, what would be the scope of the feature and how complex would be > designed type system. Examples I saw in this thread could be easily > replaced with union and intersection types (i.e. numeric as int|float). In > my opinion, there is a little benefit implementing in this shape making the > PHP core more complex. > > The two use cases of user defined types in PHP which would benefit a lot > IMO, would be: > 1. Typed arrays similar to Typescript. > 2. Semantic types which would increase the security of systems. Example: > type UserId = int; > > function setUserId_1(int $userId){} > > function setUserId_2(UserId $userId){} > > setUserId_1(5); // OK > setUserId_2(5); // TypeError > > setUserId_1(UserId(5)); // OK > setUserId_2(UserId(5)); // OK > > Kind regards, > Jorg Simple unions are the easiest to talk about in quick examples, but the real benefit of type aliases is in other cases, some of which they would enable. Example: I have a real parameter defined in my attributes library like this: \ReflectionProperty|\ReflectionMethod|\ReflectionClassConstant $subject And it appears several times, I believe. That would definitely be nicer if simplified to an alias. Example: There's general consensus that callable types would be beneficial: callable(RequestInterface, string): string or similar. But inline, that gets long and complicated fast. Type aliases would allow simplifying that to type callable(RequestInterface, string): string as PropertyRetriever function foo(PropertyRetriever $c) { ... } Example: As in the above example, type aliases serve as documentation for callables or complex types, explaining what that complex ruleset actually means, semantically. Example: If generics or typed arrays ever happen, they'd probably be useful here, too. Note that using type aliases in a required-fashion is a different matter, and potentially not feasible. Free standing variables are untyped, which means setUserId(UserId $id) could not require a UserId, because there's no way to define a variable as being a UserId, not an int. While I can definitely see a value in that kind of restriction, in practice I don't think PHP is capable of it without vastly larger changes. --Larry Garfield