Re: Generic classes and methods RFC
| From: | Ben Scholzen | Date: | Fri, 11 Sep 2015 16:55:45 +0000 |
| Subject: | Re: Generic classes and methods RFC | ||
| References: | 1 2 3 4 5 6 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-88157@lists.php.net to get a copy of this message | ||
Well, with the example, there's a good reason to have them separated:
class Factory
{
public static function createFoo<T extends \SomeClass>()
{
Attachment: [application/pkcs7-signature] S/MIME Cryptographic Signature smime.p7s
return new Foo<T>();} public static function createBar<T extends \SomeOtherClass>() {
return new Bar<T>();} } class Foo<T extends \SomeClass> {} class Bar<T extends \SomeOtherClass> {} The factory instance does not know to need about <T>, this is purely a type for the method to be able to create another class, which requires that type. You couldn't put this definition on the Factory class. On 11.09.2015 18:08, Levi Morrison wrote:
On Fri, Sep 11, 2015 at 10:00 AM, Korvin Szanto <korvinszanto@gmail.com> wrote:-- Ben Scholzen http://www.dasprids.deLevi, It's my understanding that the "type variable" is a member of the instance and not of the class. In at least java, generics are unavailable in static context[1] though they can be specifically defined on the static method itself[2]. [1] http://stackoverflow.com/questions/936377/static-method-in-a-generic-class [2] https://docs.oracle.com/javase/tutorial/extra/generics/methods.html On Fri, Sep 11, 2015 at 8:28 AM Levi Morrison <levim@php.net> wrote:This may be how it works in Java, but this is not Java. All behavior must be outlined and justified, especially because not all languages behave the same as Java with regards to generic-like behavior. Notably C++ does not share this characteristic. If this generic ability is confined only to classes/interfaces and only to instance methods that is a large limitation. Even if that is what is decided there must be rationale for choosing it to be that way.On Thu, Sep 10, 2015 at 5:12 PM, Ben Scholzen 'DASPRiD' <mail@dasprids.de> wrote:Hi Dennis, thanks for your feedback, see my answers below:I think b would be correct. Can you explain this rationale a bit more?1. static methods: class Foo<T> {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.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?
Attachment: [application/pkcs7-signature] S/MIME Cryptographic Signature smime.p7s