Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:118715 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 58815 invoked from network); 30 Sep 2022 10:16:22 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 30 Sep 2022 10:16:22 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 16A3F1804A7 for ; Fri, 30 Sep 2022 03:16:22 -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,HTML_MESSAGE, 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-yw1-f176.google.com (mail-yw1-f176.google.com [209.85.128.176]) (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 03:16:21 -0700 (PDT) Received: by mail-yw1-f176.google.com with SMTP id 00721157ae682-324ec5a9e97so39718207b3.7 for ; Fri, 30 Sep 2022 03:16:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date; bh=FOAMd4Cvbcxl1sJV59lFblEM7vlGcgUOcWXxHafYr84=; b=U6jTd94i4OTnX0WAIJqwhJWmjTt9XPYbRWbCkDKcoRJJfJ+MPRQ1F76sCYuKKPurWw I4Vgg31WbwtreRBu/kD0mntdIJnew8IKw3UIubktD0GS3nAWlnGzce9uaoih8eWlGCrO ugR4r5rVlQlMSxPxZlOAsXWHpkTAspPiRzAUEdmohuSveYOs1y42SjRJpD3hBoNMCkMS gdW+mWUrSkCbFX35gzEVaBOH2HcjMSq/QETR5LSHgdP34WXQMK7VOxZ1gn/B2NR5p8ao DLWmd4WdLRMygmcSqd/PU+RWyqs6QKsqa/9jq1z5hbfOChEEx0qZRH3Y9zLW4QrE6INB VPPA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date; bh=FOAMd4Cvbcxl1sJV59lFblEM7vlGcgUOcWXxHafYr84=; b=5ZSkBS2Seubzl4Gd2qPPe4Dj0/l7gIL/FGnYee0gdRrNnyS4S9rKWVSxcQ18Q7eBhh F7t4mctHXYfB2yr3HXtXwEsk0KH7lYU16WQPS1k1G5PlDWiQsfdUiS0d/eTRuCx0o4IR IG5mI7bMVDVM6ZpysZ+2kiW+n+7EwD+TGdsUPXf2IL1X6d7Z4yxksQBLCh9mT+ijxH2i uOlBh3j85DeC0T3Tw530LyuzG4wERf5Tq2ENTYHoCvo+GopPyVQxAkuZJsD8pDuJ1+ST nlrTIeG4JdXxmZO1wuC+MXdjzJL7D6VomfFdw5fDZlUmWuoKNbLb/yRF+BkEgsTfIEq3 tu3g== X-Gm-Message-State: ACrzQf3UGQNW3JZJp+xfcrTPeM+u/H+6XlDQy7TzKcnWbG4RRaxJZGg4 8h+AAIbgpclFne/E35UGWXWidfG9gjKtJfTF4uM= X-Google-Smtp-Source: AMsMyM4d35Fe5ISZxxXi0bTnYiojNCCzUTgrqoU6pU6Hs7c4g/CS53aZJHtEKz92zBFjWekalH4bSWSqWmx5vqJrZyQ= X-Received: by 2002:a81:9295:0:b0:353:7eab:d6cc with SMTP id j143-20020a819295000000b003537eabd6ccmr7637394ywg.232.1664532980986; Fri, 30 Sep 2022 03:16:20 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Date: Fri, 30 Sep 2022 12:15:55 +0200 Message-ID: To: =?UTF-8?Q?Olle_H=C3=A4rstedt?= Cc: Hamza Ahmad , internals@lists.php.net Content-Type: multipart/alternative; boundary="0000000000000c775a05e9e24938" Subject: Re: [PHP-DEV] Re: Alias for `int|float` From: kjarli@gmail.com (Lynn) --0000000000000c775a05e9e24938 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Fri, Sep 30, 2022 at 11: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. > > Olle > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: https://www.php.net/unsub.php > > I don't know if file based type aliases make sense, but just spitballing some ideas here. ``` // core php or perhaps some libraries typedef number =3D int|float; typedef number =3D TypeDef::extend('number', static fn (number $value, number $min) =3D> $value >=3D $min); typedef number =3D TypeDef::extend('number', static fn (number $value, ?number $min, number $max) =3D> ($min =3D=3D=3D n= ull || $value >=3D $min) && $value <=3D $max); typedef even =3D TypeDef::extend('number', stati= c fn (number $value) =3D> $value % 2 =3D=3D=3D 0); typedef positive =3D number<0>; typedef negative =3D number; typedef fraction =3D float<0, 1>; typedef string =3D TypDef::extend('string', static fn (string $value, int $min) =3D> strlen($value) >=3D $min); typedef string =3D TypeDef::extend('string', static fn (string $value, ?int $min, int $max) =3D> length_is_between($value, $min, $max)); typedef regex =3D TypeDef::extend('string', static fn (string $value, string $regex) =3D> preg_match($regex, $value)); namespace App\Domain; typedef NonEmptyString =3D string<1>; // minimum length of 1 typedef LimitedString =3D string; // no minimum length, max lengt= h of 32 // or typedef LimitedString =3D string; as alternative notation wi= th named parameters typedef SomeToken =3D regex<'/a-z0-9{12}/i'>; // only allows even numbers ranging from -100 to 100 function someFunction(even<-100, 100> $number) {} namespace Elsewhere; use App\Domain\NonEmptyString; use App\Domain\LimitedString; use App\Domain\SomeToken; someFunction(102); // fails `$max` check from `number` someFunction(-99); // fails `% 2` check from `even` ``` I tried to use some consistency for the examples, by no means this is what I think it must look like, just making sure the intent of the examples is clear. This would be nothing more than syntactic sugar to what I'd otherwise write in custom objects to validate types and values and ensure type safety. This also doesn't take into account how operators should behave or how autoloading could/should behave. --0000000000000c775a05e9e24938--