RFC: Generics
| From: | Dominic Grostate | Date: | Thu, 24 Sep 2015 20:43:05 +0000 |
| Subject: | RFC: Generics | ||
| Groups: | php.internals | ||
| Request: | Send a blank email to internals+get-88483@lists.php.net to get a copy of this message | ||
Hi Ben & Internals,
I spent most of today and yesterday just getting used to the parser
language.
I was able to implement the basic syntax on most of the constructs, just to
get a partial proof of concept going.
https://github.com/orolyn/php-src/commit/ee731240eab1e5986152cadaaca6d3e5b921ba7d
The generic patterns aren't actually used, this simply "allows" them to be
present. Also I haven't yet cleaned up the memory leaks.
The hitch I ran into however was function calls.
add_to_collection<MyClass>($collection, $object); // Sorry for the rather
pointless example.
This potentially matches an expression, therefore yacc can't use it.
Another (minor) but annoying issue is that nested generics need to be
written like:
A<T<V> > // Note the space before the last '>'
Else it will match T_SL
I don't know if either of these issues can be neatly corrected, ##parsers
told me it was impossible without some
major hackery, so this might warrant discussion on the tokens used to
define generic delimitation.
Anyway see parsable example below.
Dominic
<?php
// T = placeholder for syntax texting
interface Y {}
interface Z extends Y<T> {}
class K {}
class A extends K<T> implements Y, Z<T<T> >
{
public function add<T>(K<J> > $a): Y<T>
{
}
}
function sample<T>(A<T> $a) {
echo 'OK' . PHP_EOL;
}
sample(new A());