Hi Dennis,

thanks for your feedback, see my answers below:

1. static methods:

class Foo<T> {
        public static function test() {
        }
}

how can I call the method:
a) Foo::test()
or
b) Foo<int>::test()

in case b), would the generic methods not be a duplication?

Case a) is correct here. A static method is not in the context of an instance, so it doesn't know about the class boxing by itself.

2. Generic methods

a) Can generic methods be inside classes that themselves are not generic?

Sure, that's perfectly valid.

class Foo<T1> {
        public static function test<T2>() {
                $a = new self<T1>();
        }
}

b) Is the generic parameter type of T1 set inside the static method or
only T2? And if T1 is available, why is T2 required? What is the
advantage of having T1 and T2?

T1 is not available in the static method, as said before, the static method is not part of an instance. Furthermore, line 3 should read like this:

$a = new self<T2>();

3. Class names

a) Is it possible to have two classes with the same name (in the same
namespace) but one that requires type parameters and one that does not?

That is in fact an implementation detail, but I'd tend to say no. That is similar to function overloading, which we don't have in PHP (sadly).

b) Is it possible to allow for a generic class to be constructed without
type parameters (e.g. with mixed as the default)?

As I wrote in an earlier email, that would totally possible. My idea there was, that, as long as the types don't have restrictions (to extend another class or implement an interface), it should be allowed to construct it without a type so that it accepts any parameters.

4. Reflection

Can you get the type parameters used to construct an object at runtime? How?

That's in fact an implementation detail I can't say anything about, sorry.

5. Implementation

How do you propose to implement all this? In Java (at least in older
versions, may have changed), generics where a pure compile time
function. So the compiler checked that your code did not validate the
type but you could not access the type parameter information it at
runtime. That is not possible for PHP. So either we need classes that
can be parameterized inside the PHP core or we need "template" classes
that are compiled into concrete implementations using the type
parameters once they are used first. Have you thought about which
variant you prefer and why (or any other I did forget)? Is this even
possible without digging deep inside the language?

I haven't thought about a concrete implementation, simply because I'm not familiar enough with this part of the PHP core, and I'd leave it up to someone else to suggest the best way to implement it. But the idea of template classes sounds like a solution I'd probably chose if I didn't know better… which I don't :)

Again, thanks for your feedback!
--
Ben Scholzen 'DASPRiD'
Community Review Team Member | m...@dasprids.de
Zend Framework               | http://www.dasprids.de

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to