Re: [RFC] [Discussion] Bound-Erased Generic Types

From: Date: Fri, 15 May 2026 16:08:00 +0000
Subject: Re: [RFC] [Discussion] Bound-Erased Generic Types
References: 1 2 3 4 5 6  Groups: php.internals 
Request: Send a blank email to internals+get-130895@lists.php.net to get a copy of this message
On Friday, May 15th, 2026 at 10:56 AM, Zebulan <zebulanstanphill@protonmail..com> wrote: > How would generic types be expressed in parameters and return types? > > ```php > // Current RFC: > function DoStuff<T>(myParam: T, otherParam: int): T { > // ... > } > ``` > > Existing PHP versions will have no clue what T is. Only one of > the uses of T here is inside > <...>. Wrapping the others in > #<...> would be syntactically incoherent (and rather ugly, I > think). But the only alternative that comes to mind is: > > ```php > function DoStuff#<T>(myParam#: T, otherParam: int)#: T {} > > // which would have to look like this in projects continuing to support prior PHP versions: > function DoStuff#<T> > ( > myParam#: T > , otherParam: int // bizarre comma placement > )#: T > { > // ... > } > ``` > > Syntax parsing would, I suspect, be rather more complicated, unless you required the type to be > placed in parentheses, which would only make the syntax even less appealing: > > > ```php > function DoStuff#<T>(myParam#:(T), otherParam: int)#:(T) {} > > // and in projects continuing to support prior PHP versions: > function DoStuff#<T> > ( > myParam#:(T) > , otherParam: int > )#:(T) > { > // ... > } > ``` Whoops, my mind was in TypeScript-mode and I used suffix-style param types in my examples. Still, as Larry's comment points out, the #-style syntax is still pretty ugly (in fact, it might actually be worse) with actual PHP-style syntax.

« previous php.internals (#130895) next »