Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:130824 X-Original-To: internals@lists.php.net Delivered-To: internals@lists.php.net Received: from php-smtp4.php.net (php-smtp4.php.net [45.112.84.5]) by lists.php.net (Postfix) with ESMTPS id 0E2201A00C9 for ; Mon, 11 May 2026 07:19:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1778483952; bh=rztiHd/IgSwG7aW3yQjKOmFFkFSZhT36VliYDK4ZSPg=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=YzPRtKz19lY7y5rOMQMjZZO2BX4G4wg2mRc1M0tNF66+wVhE49X2t7o3m5BgCHrCw xOLOAPFG8+DFx+9A1g/2bpJiMRkJeRvw1NTlhz9DIsvHgTYjawx+ordB/B6sCsM+2M AfwazT4b+BnUldc5bfmp/7cupKkVP2pYGJv5rZbEULcKRt/XgaKA0qmGjKSIi+Zpxr H+WAm/iLavabLiqmGGZvN2V8uTmnkgCMJeNR1oWXs8RbdgrVQwDTBUSga/vlYQlHo/ uLOKXn8rsVbuF1sQqjG22rlXUF0Ju/fDmeYbNr1a5aC8uauGoD9ZTs0GKDq3zgEn3r Ce7H4K/dh6TZw== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id BC789180050 for ; Mon, 11 May 2026 07:19:07 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=0.6 required=5.0 tests=BAYES_50,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_MISSING, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=4.0.1 X-Spam-Virus: No X-Envelope-From: Received: from mail-ua1-f52.google.com (mail-ua1-f52.google.com [209.85.222.52]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Mon, 11 May 2026 07:19:05 +0000 (UTC) Received: by mail-ua1-f52.google.com with SMTP id a1e0cc1a2514c-95d18b33f93so2402095241.1 for ; Mon, 11 May 2026 00:19:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1778483940; cv=none; d=google.com; s=arc-20240605; b=B8AkcgUxaDetUsxCKo2ecj5CYqI3L9VMkaqRore+NZ2avmYPcnqTFvzy/5gPXiB+48 /uCXVrZLPzqXENxbOe7VX7twxIdQZZOwHr1FhFMEQQg2gkYu8F8eb3ALnLmpbJ9b0HNB ja1+BtX88/g/Lv5pWDymgbpR/yCWZTHRfnOLs6CtbsItxo2pvInqgY/Y8i9roWRbfJZM HFRCqAe54WW3taNxErOg+BU7ph3uz2t8zpA3l1YNuKzWOxgsmnseVloTNOmSoeCwQaio yNf/j/VC0PKnEUh6H1OjSITv7oJBXCl4/904VScUYLiOojvxEZcw5JmIJbEPRQOKq1T8 e7wg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=XBNpxVqAYyresMfduxHhhvIlNTnkFFZRqsrc23TmQhs=; fh=fW0orZRAntpPhjFlTeIMEldTWG/q8OibnN6RlWTdrxY=; b=BDFS7625D3b7hyDBM+9rfrFgyvJZ31Hcnlczk3ecUj/ZRxINbCi5wFAqua95VT2woc 5aLsEPvFl/qjpWBPwCZEPgGgWWLYExMBXH7M5jW6+ftG8DMzhu+Cn7TRzO90Ix9+BWtY x24i3Q8iZbjdmqLjjyWygQZ/CD9KiAAwh9v0Zv18gnU9EQpRc/NwtbpvvX0QUFO++tFJ R5KcvTcwd6GOLGUg9l8CP5Sw7u/laBYtwesd1n9M52rGQgCEKB1J6YydoS8q0RJnmuch ss/LpyLYoq1cLtaDqs0X1Kmb0JEe4NBbW7A1rtmnlo2acAqXrl95mTJnNIemjUHVZGKQ 4YfQ==; darn=lists.php.net ARC-Authentication-Results: i=1; mx.google.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=carthage.software; s=google; t=1778483940; x=1779088740; darn=lists.php.net; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=XBNpxVqAYyresMfduxHhhvIlNTnkFFZRqsrc23TmQhs=; b=D8g6/cBY7lpmfI7rl4Z6piXNGoAK5T+f6HuoYTUUOfXtKjsWoFeajN4wFEbCubSP/U DkjKYuohDNkhkrn8QmqeUbVPK7mJ9Kw7tJOymDzNzl3/YZSR+baMLL/VBfnm4pQlQTrl Lf8FqskG09ScYHljNVb/KaDpnn3arw8blf1a3nWKZv5wHwK9Pdqtl+XLtpm5W5kVwqaX legXjtgJ/keg8IBKOpnTKCX6FAIFHs1wQ+l7QvUhhZvOOL6Bm1M+TFcAqoZJb9II5WwN 0k83ms/fPzltwN65k040OiWFuCjYfaYQrKuK5RqIC+kF/WBwqUK14hppuU0Gpu0GxQEV /HeQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1778483940; x=1779088740; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=XBNpxVqAYyresMfduxHhhvIlNTnkFFZRqsrc23TmQhs=; b=sEdYV7ULjEW+LBBww/VYHMjRpivKIbx+XseKwimFwoV2kmpYEfdlNjzV0UBVe9WahG KSi78dbSzzCBUagrlISjDduXGXyYdqZGCgcPn0641MV1TTnXwe2fwcY2NRoXkpMaiy0m mtV8gNkKWZC4c3m18kVuUH0a7wVXnJ8XFdslIP68eZvLUMCT/PbmNTiWrvXKE8wNDqVY lVjWw3AROaQ+DgClcV6xOFAXXWwIQmNpYdlfa+ABHSWmH/z3HDCHQOqVuow3zbKHvIFN KbgCEzgoZJxuLHDm3TzFjqZUuXzieuWYhK49m3HU/Si9/YPUVChn+pWFe2vuUE7zm8dx KdnQ== X-Gm-Message-State: AOJu0YxCwuHYaHhWG2Sn5pDXCI3XzY7FNtiu0EdhvEkaOgxiFKMK18fz QW4UwMNyfwRgZe4A8ocj2KsFjy1vXkDBMi2QCraSQdXawbM58V/Pz1HB2/dMdjBvbN3/iGNwl8t gYpZDxYHynU6ZqXB4irbhHrtNRq2wX+HBHInNmk67Uw== X-Gm-Gg: Acq92OETAFYXlJavrN1Wl9YREvhlJZaeT6r3gEnfLoZWhfZ4hHlsivNoXS23ERxGBRj 8y8LJPP6Iiwf/qXe7U7G4E2MQkShy710hA8/8W104ATq4+7yg4TLMpp25ZMaqD2f84V+bXkkzL8 lbE44XEmQ6/OC86gpWZ8b/u6R/wRTtXnf7SvhT6R5P2v79MDRKdk/o84miJs/p05KA7EQ1Wnj8w qPSsdwTS4+vfEiaVVm/MiinLTBz8P4rv0rmTJGdCQQ5aC1XhkZfkeJpyf0xjs07ZUEyzWRjsY3f 0E105xVyUOZWaJ7HwCA4sWfyLQmr X-Received: by 2002:a05:6102:32cd:b0:634:248f:bc04 with SMTP id ada2fe7eead31-634248fbd82mr950375137.10.1778483939685; Mon, 11 May 2026 00:18:59 -0700 (PDT) Precedence: list list-help: list-unsubscribe: list-post: List-Id: x-ms-reactions: disallow MIME-Version: 1.0 References: In-Reply-To: Date: Mon, 11 May 2026 08:18:46 +0100 X-Gm-Features: AVHnY4IHi2TOPC7M2KNG6y23lYYq1APyGMc_OkJl4JgNPg4_sFfVD3y224sJXLA Message-ID: Subject: Re: [PHP-DEV] [RFC] [Discussion] Bound-Erased Generic Types To: Bob Weinand Cc: php internals Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable From: azjezz@carthage.software (Seifeddine Gmati) > > Hi Bob, > > > > > Thanks for the quick reply! > > > > Let me respond inline to avoid backtracking too much. > > > > On 11.5.2026 01:05:25, Seifeddine Gmati wrote: > > > > I have a bunch of questions and feedback: > > > > The requirement of ordering seems unnecessary to me - why would we not = want to be able to write , U: Box>. Alternatingly recursive ty= pes are not unheard of. Seems like an arbitrary restriction; and for compil= ation purposes it only requires collecting all parameter names before evalu= ating them. > > > > Your tests also show restrictions around intersection types, e.g. "Type= parameter T with bound mixed cannot be part of an intersection type" for '= class Foo {} function x(): T & Foo {}'. What's the motivation behind it?= This looks fairly natural to me: x() promises to return an instance of Foo= which also fulfills the bound T. Any child class of Foo which happens to i= mplement T will fulfill that contract. > > > > I would like to plead to skip the arity validation, except for "more pa= rameters than allowed": > > - This inhibits graceful addition of generics - any library adding them= requires callers to immediately update all caller sites. > > - It would also make addition of generics to Iterator classes etc. co= mpletely uncontroversial. > > - This would be more in line with PHP's general "no type is effectively= the highest possible bound" approach. I.e. "class A extends Box" and "clas= s A extends Box" would be equivalent. > > - This would also allow for future incremental runtime generics: you'd = start with and as you call stuff with values, the type becomes broa= der. > > > > > > This is the one thing which makes the whole RFC a non-starter for me if= required: > > > > Typing is optional in PHP! > > > > > > Your tests show that this specific example is allowed, which strikes me= as odd. Why would we not check the arity here? > > > > class Container {} > > function f(Container $x): Container { return $x; } > > > > > > Diamond checks: > > > > Are these necessarily problematic? if you inherit Box and Box, it simply means that the generic parameter, when placed in a contravar= iant location will accept int|string, when placed into return or property t= ypes it'll evaluate to never. > > > > If you disagree (that's possibly fine), a diamond covariant parameter s= hould be allowed in any case though, i.e. if Box<+T>, then an interface sha= ll be able to implement Box, Box. At least at a glance I don't= find such a test - if it already works, nice, then please just add the tes= t! > > > > > > Is class ABox implements Box allowed, or do we need to write impl= ements Box? > > > > > > I'm also not sold on the turbofish syntax. I hate it in Rust, which I h= ave to write nearly daily. I forget these :: SO often. And then the Linter = yells at me and I correct it. > > I understand that there are language limitations, in particular with th= e array syntax, but honestly, I'd rather just have the parser shift in favo= r of the existing syntax - for these rare conflicting cases forcing parenth= esis around the generic would be nicer, i.e. `[A(C)]` would continue = carrying the meaning it has today, and we'd require writing `[(A(C))]= ` for that case. > > > > > > I'm not quite sure if + and - are the proper choices. I'm more used to = C# myself with in and out being more obvious to me. I also admit that I ini= tially assumed "+" to be covariant - the sum of stuff accepted, and "-" con= travariant, subtracting what can be returned. But this particular bikesheds= color is not too important to me. > > > > > > Otherwise, it's a pretty solid RFC which should be extensible with runt= ime generics eventually. (In particular runtime generics on the class inher= itance level should be a no-brainer to add with the existing syntax.) > > > > > > Thanks, > > Bob > > > > Thanks for the careful read. Going point by point. > > > > 1. Ordering of type parameter declarations > > > > The restriction is implementation-level, not fundamental. We register > > parameter names before we compile bounds, so allowing , U: > > Box> is a "small" change. I left it out for the initial cut because > > I didn't want to bake mutually-recursive bounds into the spec without > > seeing whether anyone actually wants them in practice. If others agree > > this is worth having, I'm happy to drop the restriction before vote. > > > > Yes, that's the impression I had - an arbitrary restriction to make it = a bit simpler at compile time. > > > > I'd suggest just dropping it, why have it, actually? It should be a rel= atively easy change. I don't see any concrete advantage of this, apart from= the minor simplification this restriction would have in compiler. > > > > 2. Type parameters in intersection types > > > > The check rejects an intersection where one side is a type parameter > > whose bound is `mixed`, because the erased form can be anything, > > including a scalar. Scalars don't intersect with anything, today. ( > > ref https://3v4l.org/mdvFA#v ) > > > > The error message in the test you saw is precisely about the unbounded > > case. If `T` is bound to an object-shaped type (`T: object`, `T: > > SomeInterface`, `T: SomeClass`, ...), then `T & Foo` is allowed. the > > erased form is guaranteed to be a legal intersection operand. So this > > is the same rule PHP already enforces today, just applied through the > > erased form. > > > > Ah, I see, it needs a T: object. (or named class). It's not quite obvio= us from the error message, so I'd suggest adding a suggestion for "at least= T: object or a stronger bound" then. > > > > That makes some sense. The question would be if never types should be p= ossible to reached, but this I've basically asked already when asking about= diamond checks. > > > > 3. Arity validation at consumer call sites > > > > I think this one is a misunderstanding. Arity validation only fires > > when the caller writes turbofish. Without turbofish, nothing changes > > at the call site: > > > > ``` > > function id(T $v): T { return $v; } > > > > id($x); // no validation, no behavior change > > id::($x); // arity + bound checked > > ``` > > > > So a library can add generic parameters to its public surface and > > every existing caller (none of which uses turbofish, because turbofish > > doesn't exist today) keeps working unchanged. The validation is opt-in > > at the use site. Same for `new` and method calls. > > > > This is exactly the graceful-addition story you're asking for. The > > existing tests demonstrate it. > > > > This is not quite obvious from the RFC. I'd recommend adding a subsecti= on to "What is enforced where" detailing that these are *not* checked: I th= ought "turbofish arity" would apply to everywhere, not just explicitly wher= e the ::<> syntax is actually used. > > > > Are they also not checked for inheritance? Or just for caller sites? > > > > Sorry for missing it in tests, you have a LOT of tests! > > > > 4. Generic args on a non-generic class in a signature > > > > ``` > > class Container {} > > function f(Container $x): Container { return $x; } > > ``` > > > > This is accepted, and on purpose. PHP doesn't load classes from > > signatures, they load on use: https://3v4l.org/DnIKQ#v > > > > To validate arity at compile time, we'd have to load `Container`, > > which is a behavioral and performance regression. The cost of being > > strict here is much higher than the cost of being permissive. The same > > logic that already lets you reference an unloaded class in a signature > > lets you reference an unloaded class with type arguments in a > > signature. Validation happens once the class actually gets resolved at > > a use site (new, turbofish call, etc.). > > > > I'm actually suggesting validation at runtime here, i.e. once the class= type check passes, to check whether the arity is matching for the class of= the argument. > > > > I'm certainly not asking for compile time checks here. But leaving this= unchecked sort-of makes it the odd-one out here. > > > > 5. Diamond inheritance > > > > The diamond check is necessary because methods get substituted with > > the type arguments at link time. Consider: > > > > ``` > > interface Box { public function set(T $v): void; } > > > > class C implements Box, Box {} > > ``` > > > > After substitution, C must implement both `set(int): void` and > > `set(string): void`. PHP has no way to represent two methods with the > > same name and different signatures ( i.e overloading ), one of them > > has to win, and either choice silently breaks one of the parent > > contracts. Same problem in contravariant position. The check rejects > > this at link time rather than letting it produce a class that violates > > its own interface. > > > > For purely covariant slots you have a point, `get(): int` and `get(): > > string` could in principle be reconciled to `get(): int|string` (an > > LUB). The current implementation rejects all diamonds uniformly to > > keep linking deterministic and to avoid synthesizing union types > > during inheritance. Relaxing it for the covariant case is a reasonable > > follow-up, not something I want to bake in before vote. > > > > You got it the wrong way round, the union needs to be allowed on the pa= rameters, not the return type. > > > > set(string): void and set(int): void can be merged into set(string|int)= : void. > > > > I'd also like to mention here that: > > > > interface A { public function set(int $v): void; } > > interface B { public function set(string $v): void; } > > class C implements A, B { public function set(int|string $v): void {} } > > > > is perfectly valid today. Not allowing this for the contravariant case = would make it inconsistent with what's currently supported in PHP. > > > > This needs no overloading at all. > > > > 6. `class ABox implements Box` > > > > It is allowed and works as you'd expect. `self` resolves to the > > implementing class. > > > > ``` > > interface Box<+T> { public function get(): T; } > > > > class ABox implements Box { > > public function get(): self { return $this; } > > } > > > > var_dump((new ABox)->get() instanceof ABox); // true > > ``` > > > > Nice! > > > > > > 7. Turbofish > > > > We have to disagree here. Turbofish: > > - has zero parser conflict with comparison operators in expression po= sition > > - is uniform across `new`, function calls, method calls, FCCs, attrib= utes > > - requires no context-sensitive disambiguation rule > > > > The alternative adds a rule a developer has to learn and apply at > > exactly the worst places (inside attributes, array expressions, > > ternaries). I'd rather pay the `::` tax than introduce a > > context-sensitive parser rule that bites people inside attributes > > specifically. Rust's choice was a forced one because of `<>` overload, > > and it's the right one for PHP too for the same reason. > > > > Alright, let's disagree here. > > > > > > 8. + / - markers > > > > Picked because they don't require any new reserved words. `in`/`out` > > reads well but I'm not comfortable burning two keywords for a feature > > where two pieces of punctuation already do the job. > > > > On the "+ =3D sum of accepted" intuition: the convention here is the > > standard one from variance literature. `+` marks positions where the > > type can be widened (covariant, e.g., returns), `-` marks positions > > where it can be narrowed (contravariant, e.g., parameters). It also > > matches Hack, Scala, and Kotlin, so there is prior art the ecosystem > > already maps to. > > > > I understand, I've never used any of those languages for more than targ= eted edits, so I didn't know. > > > > I guess it's fine to not diverge here. > > > > By the way, you don't necessarily need a new keyword, in fact you could= just allow two consecutive T_STRING at that position and emit a parser err= or when the first one is neither of "in" or "out". > > > > > > Thanks, > > Bob > > > Thanks for the reply! > > 1. Ordering of type parameter declarations > > Agreed, dropped. Forward references and mutually recursive bounds > within a single parameter list are now allowed: > > ``` > function f(U $x): T { /* ... */ } // forward > class Pair, U : Box> {} // mutual > ``` > > Defaults still require backward-only references, meaning omitted > arguments resolve in one pass at instantiation. Direct self-reference > at the head of a bound (``) is still rejected; the indirect > form (`>`) is still allowed. > > Diff: https://github.com/php/php-src/compare/9ebcf28cef5563a63fe0bcc2fd5e= c45211fa1f15..f8ced4dacacda2038118bcc889f4905a92cf05de > > 2. Intersection error message > > Improved the diagnostic to point directly at the fix instead of just > stating the rule. The message now reads: > ``` > Type parameter T with bound mixed cannot be part of an intersection > type; use an object-shaped bound (e.g. T: object) > ``` > > Diff: > Thanks for the reply! > > 1. Ordering of type parameter declarations > > Agreed, dropped. Forward references and mutually recursive bounds > within a single parameter list are now allowed: > > ``` > function f(U $x): T { /* ... */ } // forward > class Pair, U : Box> {} // mutual > ``` > > Defaults still require backward-only references, meaning omitted > arguments resolve in one pass at instantiation. Direct self-reference > at the head of a bound (``) is still rejected; the indirect > form (`>`) is still allowed. > > Diff: https://github.com/php/php-src/compare/9ebcf28cef5563a63fe0bcc2fd5e= c45211fa1f15..f8ced4dacacda2038118bcc889f4905a92cf05de > > 2. Intersection error message > > Improved the diagnostic to point directly at the fix instead of just > stating the rule. The message now reads: > ``` > Type parameter T with bound mixed cannot be part of an intersection > type; use an object-shaped bound (e.g. T: object) > ``` > > Diff: https://github.com/php/php-src/compare/f8ced4dacacda2038118bcc889f4= 905a92cf05de..6b5588d8e927d60e4b4509658af62601dfa0802a > > 3. Arity validation at consumer call sites > > You're right that the RFC didn't say this clearly. Added a "What is > not checked" subsection under "What is enforced where" that lists the > exact sites where the engine intentionally omits arity or bounds > validation. The whole point of opt-in is the graceful-addition story > you're after; the RFC now spells that out. > > 4. Runtime arity check at call boundaries > > In principle, sure. Once the runtime confirms the value matches the > class, we can also validate the signature's type arguments against the > class's actual declared arity and bounds. So in: > > ``` > class C {} > function foo(C $x): void {} > > foo(new C()); > ``` > > we'd error because `C` has no generic parameters but the signature > supplied two type arguments. > > The catch is that this isn't only about parameters. It applies to > every place the engine resolves a class-typed type expression at > runtime. > > So a bit more complicated, not a small change. I want to spend more > time on it before committing to text: what exactly gets validated, > where the result gets cached so we aren't paying for it on every typed > call, how it interacts with the substitution chain at link time, and > what the hot-path cost actually is on a profiled workload. I'd like to > see what others on the list think too, since the call is a trade-off > between strictness and performance and people will weigh those > differently. > > If after that the answer is "yes, fold it in", I'll fold it in. But I > don't want to promise it inside this RFC until I've done the > investigation. > > 5. Diamond inheritance - I had the direction wrong > > Yea, sorry. Contravariant (parameter) positions are the ones that > merge cleanly into a union, not return positions. Your example is > right. > > For the generic case, the contravariant side is the easy one: > ``` > interface Box<-T> { public function set(T $v): void; } > > class C implements Box, Box { /* set(int|string) */ } > ``` > > The implementer's substituted prototype is the union of the two > contravariant slots. > > The covariant side is more nuanced. `get(): int` and `get(): string` > merged would have to return both ( i.e. `int & string` ), and PHP > rejects intersections involving scalars (because impossible!). So a > covariant diamond with scalar bindings is unrepresentable. It only > becomes representable when the type parameter is bounded by an > object-shaped type, in which case the implementer's return type > collapses cleanly to an intersection: > > ``` > interface Box<+T : object> { public function get(): T; } > interface A {} > interface B {} > > class C implements Box, Box { > public function get(): A&B { /* ... */ } > } > ``` > > Here `A & B` is a valid PHP intersection, so the merge is sound. > > I'll look into this. I think we can fit it into this RFC, but I want > to investigate the implementation first. I keep the RFC and the > implementation in sync and don't want to commit to text that isn't > backed by working code yet. > > Thanks, > Seifeddine. > > 3. Arity validation at consumer call sites > > You're right that the RFC didn't say this clearly. Added a "What is > not checked" subsection under "What is enforced where" that lists the > exact sites where the engine intentionally omits arity or bounds > validation. The whole point of opt-in is the graceful-addition story > you're after; the RFC now spells that out. > > 4. Runtime arity check at call boundaries > > In principle, sure. Once the runtime confirms the value matches the > class, we can also validate the signature's type arguments against the > class's actual declared arity and bounds. So in: > > ``` > class C {} > function foo(C $x): void {} > > foo(new C()); > ``` > > we'd error because `C` has no generic parameters but the signature > supplied two type arguments. > > The catch is that this isn't only about parameters. It applies to > every place the engine resolves a class-typed type expression at > runtime. > > So a bit more complicated, not a small change. I want to spend more > time on it before committing to text: what exactly gets validated, > where the result gets cached so we aren't paying for it on every typed > call, how it interacts with the substitution chain at link time, and > what the hot-path cost actually is on a profiled workload. I'd like to > see what others on the list think too, since the call is a trade-off > between strictness and performance and people will weigh those > differently. > > If the answer after that is "yes, add it,", I'll fold it in. But I > don't want to promise it inside this RFC until I've done the > investigation. > > 5. Diamond inheritance > > Yea, sorry. Contravariant (parameter) positions are the ones that > merge cleanly into a union, not return positions. Your example is > right. > > For the generic case, the contravariant side is the easy one: > ``` > interface Box<-T> { public function set(T $v): void; } > > class C implements Box, Box { /* set(int|string) */ } > ``` > > The implementer's substituted prototype is the union of the two > contravariant slots. No new type-system rules are needed. > > The covariant side is more nuanced. `get(): int` and `get(): string` > merged would have to return both ( i.e. `int & string` ), and PHP > rejects intersections involving scalars (because impossible!). So a > covariant diamond with scalar bindings is unrepresentable. It only > becomes representable when the type parameter is bounded by an > object-shaped type, in which case the implementer's return type > collapses cleanly to an intersection: > > ``` > interface Box<+T : object> { public function get(): T; } > interface A {} > interface B {} > > class C implements Box, Box { > public function get(): A&B { /* ... */ } > } > ``` > > Here `A & B` is a valid PHP intersection, so the merge is sound. > > I'll look into this. I think we can fit it into this RFC, but I want > to investigate the implementation first. I keep the RFC and the > implementation in sync and don't want to commit to text that isn't > backed by working code yet. > > Thanks, > Seifeddine. Hi Bob, Pulled an all-nighter on #5 because the more I thought about it the more it seemed like the right thing to do, and it turned out to compose really cleanly with the existing parametric LSP machinery. Both directions are now in: Contravariant diamond -> union merge Covariant diamond on object-bounded T -> intersection merge The RFC has a new "Diamond inheritance" subsection covering both: https://wiki.php.net/rfc/bound_erased_generic_types#diamond_inheritance Genuinely happy with how this turned out. Example from the RFC: ``` interface Renderable {} interface Cacheable {} class Article implements Renderable, Cacheable {} interface Pipeline { public function process(T $value): T; } interface RenderingPipeline extends Pipeline {} interface CachingPipeline extends Pipeline {} class ArticlePipeline implements RenderingPipeline, CachingPipeline, Pipeline { public function process(Renderable | Cacheable $value): Renderable & Cacheable { if ($value instanceof Renderable && $value instanceof Cacheable) { return $value; } return new Article(); } } ``` The synthesized prototype across the three views is `process(Renderable | Cacheable): Renderable & Cacheable`. The merge is computed at link time against PHP's existing union and intersection operators, so no new type-system rules are needed. Thanks for pushing on this :D Cheers, Seifeddine.