Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:121478 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 96602 invoked from network); 27 Oct 2023 01:53:28 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 27 Oct 2023 01:53:28 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id AF3FB180212 for ; Thu, 26 Oct 2023 18:53:27 -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-vk1-f179.google.com (mail-vk1-f179.google.com [209.85.221.179]) (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 18:53:27 -0700 (PDT) Received: by mail-vk1-f179.google.com with SMTP id 71dfb90a1353d-49d45964fcaso1706935e0c.1 for ; Thu, 26 Oct 2023 18:53:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1698371606; x=1698976406; darn=lists.php.net; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=KyNuu7meaEDyspOfo3SQDB+LguZLXP98lh3fZ4KMISo=; b=DVDV4fEinL/DlW2Mc6t9hAeQkWuyyI9lCXBuyTBGXzV2km3lFCQC1RBz15dFrmkYPO ZkH7Jnygs8ER7lCUmJJv1bdsfBB3WkkoEMjXlsag7JpBrQvgF+TyeL3eIHUYS3tnPZKU B2M5aFthIsjMtLO2bFv6NSHjP3eIBDaDs79/xR8n7gZ9b/owcYILOWCMA97IaGbn8Xtz e6kYgb0TMS8S8mEs8ZCCnBVyH6QhwUpEvbZAAlQSTSPZ4/LXbmi8wgIQEvCVL7S3JDnX rZkxWShjCJukT9mbHGv2xlMv1Uz2IvA/Qz1BL/V4+lLPkQEBDlYnJzPmLJPmauhfFUS9 m0NQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698371606; x=1698976406; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=KyNuu7meaEDyspOfo3SQDB+LguZLXP98lh3fZ4KMISo=; b=O36m7ZlnqerS9CMwJAXdZwX2akOdsxALFbvGbKVRm7+fmt9INi7XbVLRMDWQP2Is2i PXzeXFEQcTPkTHP73wbgk8tmtrbL9Fcg/2ChJu8mcwaAwqMOpT9N6dEWN65FfoK7SpR/ KM8xed3KzoEmevYpNSqDs2hYLpNX96OanYROXEQ93paN3vDBXq793BxBBKCf5GghuXEE cXdysvqfQkJlfvzvtznN4JCmReQD6SFnfbuZVFLAYqWyv2djNZZWY7RIx2pEtnbM7xp0 yZOKA5+Ul7DKZPX5O/5nqWeBeBUVr9J0GEMfJGqFfIGl7kmQ/4y4Dum55np5Rf3zcne/ NRTw== X-Gm-Message-State: AOJu0Yzsjhuf13wbYrXA+qMw0VpGzoR4G5OnK4DAbBjSos/u3oYsUisq Bdc3DWIPEv0DZoUmL2goQfVrMClEvkJ0zVblaH8= X-Google-Smtp-Source: AGHT+IEyyVDOX8b20oZ57xb/bFADLV73tS0H/kSUSypTVpQBPgMcZwUqGfg5eZ1VH8yObe0J+QV+mpgZLYFbxLnIl2M= X-Received: by 2002:a1f:2a0e:0:b0:49a:a6c8:c815 with SMTP id q14-20020a1f2a0e000000b0049aa6c8c815mr898712vkq.6.1698371606166; Thu, 26 Oct 2023 18:53:26 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Date: Fri, 27 Oct 2023 02:53:13 +0100 Message-ID: To: Erick de Azevedo Lima Cc: Deleu , Larry Garfield , php internals Content-Type: multipart/alternative; boundary="0000000000004846080608a8f491" Subject: Re: [PHP-DEV] Basic Type Alias From: oladoyinbov@gmail.com (Oladoyinbo Vincent) --0000000000004846080608a8f491 Content-Type: text/plain; charset="UTF-8" Since there may be need for reusability, I feel it can have this kind of structure: // File: A.php ``` namespace A; typedef TypeLists { type Msg = string|null; type Numeric = int|float; type MyClass = \Package\Test|\Package\Db; } ``` // file: B.php ``` declare(strict_types=1); use A\TypeLists; class Test1 { use TypeLists; public function greet(Msg $message): Msg { // ... } } $init = new Test1; // it triggers TypeError when strict types is declared $init->greet(1); // TypeError ``` And for local usage only: ```php typedef T = int|string|null; // or type T = string|int|null; $msg = fn (T $message): T => $message; echo $msg; ``` On Fri, 27 Oct 2023, 12:51 am Erick de Azevedo Lima, < ericklima.comp@gmail.com> wrote: > I'll use it a lot if they'll be namespaced. It avoids name-collision using > the existing ecosystem and we already use lots of "uses" in our codes. > When Enums came to the party, they came namespaced as traits, classes and > interfaces. The last 2 represent types, concrete and abstract. > User defined types are nothing more than types, so I think the way to gois > to keep it namespaced as well. > > About the type compatibility, covariance and contravariance, let's say I > define some type inside my package: > > // File: C1.php => My package code > > namespace N1; > > typedef MyString = > string|Stringable|\Symfony\Component\String\UnicodeString > ; > typedef MyBool = null|bool; > typedef BigNumeric = int|float|\Brick\Math\BigNumber; > > class C1 > { > protected function myQuestionableFunction(MyString $str1): MyBool > { > //... > } > > protected function myQuestionableFunction2(null|C1 $str1): > ScalarNumeric > { > //... > } > } > > // File: C2.php => On the project of those who are using my package > > namespace N2; > > typedef ScalarNumeric = int|float; > typedef Falsy = null|false; > > class C1 extends C2 > { > protected function > > myQuestionableFunction(string|Stringable|\Symfony\Component\String\UnicodeString > $str1): Falsy > { > //... > } > > protected function myQuestionableFunction2(Falsy|C1 $str1): > ScalarNumeric > { > //... > } > } > > I propose that this code should be valid code, because, in the end, these > *typedef's*, are just synonyms. > > *Note:* By the way, I propose to use the C/C++ keyword, which is *typedef* > and > dropping the syntax* "type MyType = [string, null];",* > which differs too from how typehints are used and do not add any benefits > *.* > > -- > Erick > --0000000000004846080608a8f491--