Re: Generic classes and methods RFC

From: Date: Mon, 07 Sep 2015 09:52:56 +0000
Subject: Re: Generic classes and methods RFC
References: 1 2 3  Groups: php.internals 
Request: Send a blank email to internals+get-88083@lists.php.net to get a copy of this message
Hi, On 05.09.2015 21:48, John LeSueur wrote:
Hi Ben, Now that I have substantive questions, including the list :) In the following: class SomeList<Bar> {
     public static function fromArray<Baz>(array $a): SomeList<Baz> {
         $list = new SomeList<Baz>();
         $list2 = new SomeList<Bar>();
         return $list;
     }
} $list = SomeList<int>::fromArray([1, 2]); When fromArray() executes, what type is $list2? or is that an error? If it's an error, what advantage is there to having distinct generic types on the class vs. the method? How do I specify the two generic types I want to use?
The $list2 line would definitely be an error, since the <Bar> hint is not known in the static context. The advantage is simply that you can pass a type through the a static method to create an object with a specific type for you.
Another thing I'd like to make sure I understand, how does this interact with strict types (and non-strict)? new Entry<int, string>(1, 2);//I assume it should type-juggle declare(strict_types = 1); new Entry<int, string>(1, 2);//I assume it should throw error
I didn't think too much about this, but I guess you are right that for scalar types, the strict_type declaration should apply in the user context.
I'm interested to see if PHP can get generics going. I hope hashing out some more details might garner more interest. Thanks, John
-- Ben Scholzen http://www.dasprids.de

Attachment: [application/pkcs7-signature] S/MIME Cryptographic Signature smime.p7s
« previous php.internals (#88083) next »