Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:118717 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 75990 invoked from network); 30 Sep 2022 14:53:08 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 30 Sep 2022 14:53:08 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id CC6FF1804BA for ; Fri, 30 Sep 2022 07:53:07 -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-lf1-f48.google.com (mail-lf1-f48.google.com [209.85.167.48]) (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 07:53:07 -0700 (PDT) Received: by mail-lf1-f48.google.com with SMTP id c2so888492lfb.10 for ; Fri, 30 Sep 2022 07:53:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:to:subject:message-id:date:from :references:in-reply-to:mime-version:from:to:cc:subject:date; bh=yTkkePYl1PErpnjSHi6hwZA4M408wHpRs6mGak5eH3U=; b=CrSofnuMg0m3ySM0UUR4eDd8IM/qhumTbpZw4QtsBL2teDlrv+spqKIVxxpnMPNuyP ekDt0//dyRPxIfOxx8/JUQd2wLdB/EIKK7hSeg9NiXKylDs3oZtjcyJCK02U+LtfPTbc zRrEuWov2soXFN7wM7+MA5Axz70wyjoYRw7AhTCmJERay2FHeUSezs/mfzD0uZESHoKv sfn1OS0gOmFVVZcOWDMryEiGWS3OrRsBMg+oSbKlER5gbYfqrNtEEGmsmIRP6nve/gv1 pceP0S3GsEadajIPxq+1g1wSjAbo7AhnUnwgq7F5VdLtgLsc5chO5lY3785IKmc9S9+x drkA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:to:subject:message-id:date:from :references:in-reply-to:mime-version:x-gm-message-state:from:to:cc :subject:date; bh=yTkkePYl1PErpnjSHi6hwZA4M408wHpRs6mGak5eH3U=; b=GVsFV+jTW7ZSn4U6jzbcYDASWHL4nXvr+azPoaywKFJFB0SgVi/TNNi4mU1uCxujfO 4nlPYcKkJOPUdMqHFdtKvYH1mn+Mk2Rf3TsCxhy0HWQuWNkbVKqBqcGr8UOFzRwcULpQ lgrHyhjc9cp5SVuGD7N8ao1T/Ftb+XxMqO28wU05OppxnUGk05nVTSa3B0sSf8iQ51c6 m9ef7xD2EWQEtSCROaiOhCUb27loi6To/Khw1dFko7Rx68RCb38Ca9dinQDVUXU4w0Vh Kr9I2Ai+W5VcovFHOefk0oaqJ7LQT5EXvkEPx88LnZF5Jk/omtcdHZubqiB59zGWuU9R /yww== X-Gm-Message-State: ACrzQf1rhyfB0ABijHyBKRDOtDMx//hxt+SE4JziVawXXdrv2c9/vdey QPHwNW1gHpNoUkEuh+OS/1S6/u3gEvfXk4cmYI0= X-Google-Smtp-Source: AMsMyM4pCKq3gJqJVXMP/x7UdO+9TmyyTglGsYw+xllujembNZUik7q9KxNB++AUbZjmMkjp+ssOBw0YTuTtvOII7HQ= X-Received: by 2002:a19:4f45:0:b0:49a:63c9:190d with SMTP id a5-20020a194f45000000b0049a63c9190dmr3250099lfk.61.1664549585983; Fri, 30 Sep 2022 07:53:05 -0700 (PDT) MIME-Version: 1.0 Received: by 2002:a2e:5303:0:0:0:0:0 with HTTP; Fri, 30 Sep 2022 07:53:05 -0700 (PDT) In-Reply-To: References: Date: Fri, 30 Sep 2022 16:53:05 +0200 Message-ID: To: Hamza Ahmad , internals@lists.php.net Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Subject: Re: [PHP-DEV] Re: Alias for `int|float` From: olleharstedt@gmail.com (=?UTF-8?Q?Olle_H=C3=A4rstedt?=) 2022-09-30 12:15 GMT+02:00, Lynn : > 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 (numb= er > $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= null || > $value >=3D $min) && $value <=3D $max); > typedef even =3D TypeDef::extend('number', sta= tic > 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 len= gth > of 32 > // or typedef LimitedString =3D string; as alternative notation = with > 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 wha= t > 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. Impressive repertoire of ideas, but probably out of scope from what I was thinking of. :) Some call this "refinement types": https://en.wikipedia.org/wiki/Refinement_type Olle