Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:116190 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 98743 invoked from network); 3 Oct 2021 05:35:14 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 3 Oct 2021 05:35:14 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 21A8F1804C5 for ; Sat, 2 Oct 2021 23:19:04 -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=-1.7 required=5.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL, SPF_HELO_NONE,SPF_NONE 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-pl1-f179.google.com (mail-pl1-f179.google.com [209.85.214.179]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Sat, 2 Oct 2021 23:19:03 -0700 (PDT) Received: by mail-pl1-f179.google.com with SMTP id bb10so9783plb.2 for ; Sat, 02 Oct 2021 23:19:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=dqxtech-net.20210112.gappssmtp.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc:content-transfer-encoding; bh=kqRiPLkcvGxT96dif3kD84Qp8vW5SzF6h1YX/etvvsU=; b=qu7qpaObL/1UHyj6Rtqr2S6eO2T5S+WACva59qIYOW/fNZB0xT9WYE/ulVPptLjbwh Qj14LieAIfiJ/SArVOmhEJNSFlPi6vVQ152RzAjA3zXtOvSDw1YrBT21rDMWfr+4gBmf k9fdhAH2jodTAb0oc45W8VgH/o0CZ8TknuGidZMWcv6a7E1ofRRS6fQvsf8nG9MLsag8 6PSiJqx+EEEaZD9yzkgyLn2bfUySQAOrKv/m1xfC/taHk8F9NzgxssHMPWNW3SnXAp0y DCw3JMcjLClGc2eQSKZlZH9xOyar2H7erFt69BilThT4/FhF0zBQ80TDHKe18zIIr3uS uopQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc:content-transfer-encoding; bh=kqRiPLkcvGxT96dif3kD84Qp8vW5SzF6h1YX/etvvsU=; b=VgMWCGk1L1Wp8VR0fJa2LOkCqhzKEWnN2nf/MuAylEKaNZDEWUd+fw2qk3ib/S7MgG Ymw2mok3si2XGULsMBFkuL/YrJC0y4mPr4NviLupqu84NNkPhLYdTo4wa31Cr4ui0JMh RYZ+f/qo4KJzS5ck+7cN4PiG5fSob98JYzX1Z3e3e0J/ZmhNkCjsR47+x3p2tCYPC2IG +puNWJJ67CnR8+cod4fNB7dm3wTj3yIt7Af2XHMy7uXg9TT010ZbB0FHjSvKFbRH30pD 7BRioJV7k2CvXrNUvryFRB7+5HfswGil9maZ93hW8UJVJUNvKsQNponbJ7QANAJ7xomy qktg== X-Gm-Message-State: AOAM533mI8pARnJFdUZM03GiA5SBnpRRkEa5Ra/H9PHyZgANRY4ppdxo f4XUlyUNzmge4gJwJG0QKFEkUHjYcqTBG/fiIH0L/MrbiF5CVA== X-Google-Smtp-Source: ABdhPJzjRsjB0PXkR14Wsu/MBzKfaghUnpxNNcu5lSE7P8/4xc9glDlit58od+Q+4I1gxWXXc3zTFkHEGSCCm9GR9D8= X-Received: by 2002:a17:90a:1944:: with SMTP id 4mr28944580pjh.221.1633241938713; Sat, 02 Oct 2021 23:18:58 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Date: Sun, 3 Oct 2021 08:18:47 +0200 Message-ID: To: tyson andre Cc: PHP internals Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Subject: Re: [PHP-DEV] Unified ReflectionType methods From: andreas@dqxtech.net (Andreas Hennings) On Sat, 2 Oct 2021 at 16:37, tyson andre wrote: > > Hi Andreas, > > > Hello list, > > I would like to propose new methods for ReflectionType, that would > > allow treating ReflectionNamedType and ReflectionUnionType in a > > unified way. > > This would eliminate the need for if (.. instanceof) in many use cases. > > > > Some details can still be discussed, e.g. whether 'null' should be > > included in builtin type names, whether there should be a canonical > > ordering of type names, whether we should use class names as array > > keys, etc. > > ... > > What do you think? > > Relatedly, I also had different ideas lately about new methods for Reflec= tionType, though of a different form. > > 1. To simplify code that would check `instanceof` for all current and fut= ure types such as `never` and `mixed` and intersection types > `ReflectionType->allowsValue(mixed $value, bool $strict =3D true): bo= ol` > > Maybe also `allowsClass(string $className, bool $strict =3D true): boo= l` to avoid needing to instantiate values (weak casting allows Stringable->= string). Ok for me, why not. I would see it as a distinct PR, separate from my original proposal. The problem or limitation of these methods is that they don't reflect the full expressiveness of the type system. That is, the return values of these methods would not be sufficient to recreate an equivalent type. > 2. To simplify code generation, e.g. in mocking libraries for unit testin= g: `ReflectionType->toFullyQualifiedString(): string` (e.g. `\A|\B`) (may n= eed to throw ReflectionType for types that can't be resolved, e.g. `parent`= in reflection of traits, keep `static` as is) > > (The raw output of `__toString()` isn't prefixed with `\` (e.g. `A&B`= ) and can't be used in namespaces Again, if we iron out the details, it can be a good idea as a distinct standalone PR. > > The fact that both intersection and union types (and possibility of union= types of full intersection types) > make it hard for me to believe that getBuiltinTypes and getBuiltinClasses= would be used correctly when used. Just to mention it, my proposed names were getBuiltinNames() and getClassNames() to be super clear that the return value will be string[]. For future intersection types, there could be additional methods getBuiltinIntersectionTypes() and getClassIntersectionTypes(), or it would be a single method getIntersectionTypes(). Not sure yet which combos make sense. E.g. array&callable would combine two builtin types, Countable&callable would combine a class-like type and a builtin type. Every type would be broken into a normal form, where the top-level is treated as a union type, and each item can either be a pure builtin type, a pure class type, or an intersection of pure types. int|string|(A&(B|(C&D))) =3D=3D=3D int | string | (A&B) | (A&C&D) If we always use this normal form, then the ReflectionIntersectionType only needs to have getBuiltinNames() and getClassNames(), not getUnionTypes(). Not sure yet what to do with 'static' and '$this'. > > Thanks, > Tyson > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: https://www.php.net/unsub.php >