Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:118720 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 24709 invoked from network); 1 Oct 2022 02:33:42 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 1 Oct 2022 02:33:42 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id B2979180210 for ; Fri, 30 Sep 2022 19:33:41 -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.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS15169 209.85.128.0/17 X-Spam-Virus: No X-Envelope-From: Received: from mail-vs1-f43.google.com (mail-vs1-f43.google.com [209.85.217.43]) (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 ; Fri, 30 Sep 2022 19:33:41 -0700 (PDT) Received: by mail-vs1-f43.google.com with SMTP id n186so748636vsc.9 for ; Fri, 30 Sep 2022 19:33:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:cc:to:subject:message-id:date:from :references:in-reply-to:mime-version:from:to:cc:subject:date; bh=LryDFXtcB74Iy/WtD0co3D9jc+fopzZdDgSf6OighsU=; b=Or11C67YyJYJjd+ZH/wW4UYWRVVOKv4hr1188KkB0I2FVRqUfTugJeBkYojwYDmghU jL525WPw06eMfspA4wv5n68mLwUyFU8v/Bozd23YdSU8KMz4DqSfEwTT6magUzCY6K6l euAvtZGwNdUTrMtfONo00kfb07Fc0s8HJH3WbgM7oPC+Gs3ANkKzh/PjZ6K6CruLSgA1 on8m7kwlDNow6Kj2/kIUmFhCRttzejZ25wVkVddvjnstnMXu0XNuhwh4jASjHIrwNfWE Xi6TAYKkhsGtN+Szq33Qfsbs5uVy71pwHwvGqPFul6+1ecW4PTx/UMBKEbq7tBcJAyLB J0Aw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:cc:to:subject:message-id:date:from :references:in-reply-to:mime-version:x-gm-message-state:from:to:cc :subject:date; bh=LryDFXtcB74Iy/WtD0co3D9jc+fopzZdDgSf6OighsU=; b=ikapCvWdCGHqxe6iMXhzxg8ajHwOAZ83CIGKQsOxYKRZcTmfUPeytTnf0zwMjANZhP hpDxORcCm13lMSgE6bCq+9tXLcsT7LOTl8HAx3g17FDDmr5LNOmIxF2tbb5WWrdrFcSd Ln+MLCTKCa4Msxnqcq2vqVlwdxE5WwBRknnY75VO7+PsEvebkwLqitbg73Ff5ngPeWpo ICk7GRQiLizyEqdiMpdXilt1RHtQyGnTobTKvFMPPsPz3GNKQA0i5cD4ZGLfVx4UfvUx MF0/97xKhn1BoO0DjDudCctN+cSIyGFvbPJ03nXxeMmmCtEvLkFyeY6YQ29MWdEckjqq gXNQ== X-Gm-Message-State: ACrzQf3QlE3TB+5jW84Y4JvhEvKPn7bknSmkX8iWiMAsTBUGTztKNH0t LUZUw6N5nZMybCIhU+YZm2t9wC+iUNNK7UaEyk8= X-Google-Smtp-Source: AMsMyM4BcXDQXw2/B0O099ePEpdJkn56wJyZHnH9tBbMrHYKSa4AN4E2Xvlrjkq7N/KCCt1PaI7eX8zQxS1losHf1ro= X-Received: by 2002:a05:6102:e11:b0:398:16c6:ee2a with SMTP id o17-20020a0561020e1100b0039816c6ee2amr5699277vst.2.1664591620479; Fri, 30 Sep 2022 19:33:40 -0700 (PDT) MIME-Version: 1.0 Received: by 2002:ab0:77d5:0:b0:3bb:5df4:fc55 with HTTP; Fri, 30 Sep 2022 19:33:39 -0700 (PDT) In-Reply-To: References: <38350298-6aaa-4739-b927-0a9423baae4e@app.fastmail.com> Date: Sat, 1 Oct 2022 07:33:39 +0500 Message-ID: To: =?UTF-8?Q?Olle_H=C3=A4rstedt?= Cc: Larry Garfield , php internals Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Subject: Re: [PHP-DEV] Re: Alias for `int|float` From: office.hamzaahmad@gmail.com (Hamza Ahmad) After reading through all of the suggestions above, I have come to this conclusion that all suggestion can be incorporated in the following ways: Types can exist in name spaces, Either in the global namespace or in the user defined name spaces. If one wants to import a type from a name space, it can easily use "use" statement per file. Alternatively, it can include a namespace and it does all that handy job for the dev. I agree with the idea of creating an RFC and document all use cases, possible ways to implement and also the features that can be added currently or will be left for the future scope. Regards Ahmad On 9/30/22, Olle H=C3=A4rstedt wrote: > 2022-09-30 17:16 GMT+02:00, Larry Garfield : >> On Fri, Sep 30, 2022, at 4:04 AM, Olle H=C3=A4rstedt wrote: >>> 2022-09-29 5:08 GMT+02:00, Hamza Ahmad : >>>> Hi Olle, >>>> >>>> I appreciate your idea of introducing a similar concept of typedef. >>>> What if you write an RFC explaining that concept. I can join you >>>> however in co-authoring this request. >>>> >>>> Seriously, I have had issues with writing such type again and again. >>>> If you look at mixed type, it is indeed an alias of some union types. >>>> Adding such an option to userland would lead to introduction of >>>> various type hints. Plus, it will help devs write short hand aliases >>>> for their intersection types. >>> >>> Note that my suggestion here is NOT about a project-global type alias, >>> but an extension of the existing file-only alias, the use-statement. >>> This can also explain the lack of reaction. :) Internal devs might >>> already have consensus on the right way to proceed here, even if >>> there's no RFC yet. >> >> Background: >> >> Type aliases have been discussed several times. > > Ya ok, not what I could find in externals.io ^^ > >> There's definitely >> interest, but like many things it runs into the problems of time and >> figuring out the details. >> >> In particular, I don't get the sense that there's much appetite for >> file-local aliases. While that would have some benefit, it could lead t= o >> all kinds of confusion when then using code from another file. Consider= : >> >> A.php: >> >> use int|float as number; >> >> interface A >> { >> public function a(number $a) {} >> } >> >> B.php: >> >> // Yeah, aliasing this is very helpful. >> use (Request&Linkable)|(ServerRequest&Linkable)|array|null as input; >> >> interface B >> { >> public function b(input $b) {} >> } >> >> C.php: >> >> class C implements A, B >> { >> // Which of these to use? >> public function a(int|float $a) {} >> public function a(number $a) {} >> // Or do I need to repeat the definition of "number" in every file? > > Yes, you would. That's the limit of my current suggestion. Same > semantics as use-statements. > >> // WTF do I do here? Do I have to repeat the entire definition either >> // inline or in this file's "use" block? Both suck. >> public function b(...) > > Neither suck. :) Just "normal" inheritance boilerplate in the absence > of project-global type aliasing. > > Do you have similar counter-examples for non-OOP code? > >> } >> >> >> This becomes even more of an issue when you start talking about function >> types, which is often mentioned as the primary use case. >> >> type fn(int $a, string $b): Request as mycallback >> >> function do(mycallback $callback) {} >> >> So I have to duplicate the type def in every file now? This is not an >> improvement. > > It's an improvement for readability, especially when repeated in one > file. But my suggestion does not include function-types, because > there's no such thing already in the system. Just to let use-statement > include more of already existing types. If function-types were added > later, they should be supported too, yes. > >> >> So we have to go with app-global aliases, but then... how do you handle >> resolution? Autoloading? Namespacing? Conflict resolution? I dunno, >> that's all hard, and that's where the conversation usually peters out. > > Yeah, always better to have a clear limitation, is it not? :D > >> The degenerate case you mention of simple type renaming has its own >> interesting implications, which are also a factor for the broader case. >> Specifically, does that create a new type unto itself > > No. Keep the same semantics of the current use-statement. But it could > be a future extension, sure. But then you need some kind of "factory" > way to produce those types. In FP they do it with "abstract types", > where the implementation of a type is hidden from outside the module. > In PHP, maybe with Foo::make() functions or such. Different > discussion. > >> or does the >> difference get elided at compile time? For instance: >> >> use int as userId; >> use int as productId; >> >> function a(userId $user) { >> b($userId); >> } >> >> function b(productId $product) { >> // ... >> } >> >> a(123); >> >> >> Does this work? Is it a compile error for mismatched types (as in Go, >> which >> has this exact feature) or no? If it is, how do you make it work? Do >> you >> need explicit casts? If it does work, have you gained much since it's n= o >> more useful than docblocks? If it does work, how long before SA tools >> turn >> it into an error anyway and make everything more complicated? (Because >> you >> know they will.) > > Not sure I agree with you there. Obviously, static analyzers would > have to abide to the semantics of the core language, no...? > >> Does the same answer apply for longer type aliases like the nutty ones i= n >> the earlier examples? >> >> These are the kinds of questions that need to be answered before any >> serious >> RFC could be attempted. > > Isn't an RFC a good place to document all these cases? I was thinking > that an RFC could be a good place even for things we do NOT want to > implement. to help pave the way for features we do. > >> Answers for them can certainly be found, but so far >> no one has really done the work, in particular around how to make aliase= s >> cleanly global. If you're interested in tackling that, fantastic; I >> think >> most people would be on board with it (especially for function types). >> But >> be aware it's a not-small puddle you're wading into. > > I never had a need for such a feature tho. :d Although my motivation > for current concept is also dubious (transpilation to C, mainly). > Maybe generic-notation for arrays is more relevant to improve > type-hints, buuut that's another can of worms, and I didn't read up on > current discussion yet. :) > > Thanks for the feedback, Larry! Can I still get karma to write it down > in the wiki? Again, not to necessarily encourage implementation, more > for documentation purpose. And I prefer to have it on the wiki, to > keep all the docs central. > > Olle > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: https://www.php.net/unsub.php > >