Re: Type hinting
| From: | Ferenc Kovacs | Date: | Tue, 01 Jun 2010 20:27:39 +0000 |
| Subject: | Re: Type hinting | ||
| References: | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-48598@lists.php.net to get a copy of this message | ||
On Tue, Jun 1, 2010 at 10:11 PM, Stas Malyshev <smalyshev@sugarcrm.com>wrote:
> Hi!
>
>
> Also, it never makes sense to convert one object type into another, and
>>
>>> almost never this operation can be defined.
>>>
>>
>>
>> array and ArrayObject?
>>
>
> This is a good example because strict typing would probably reject
> ArrayObject passed as array, thus defeating the whole purpose of having
> ArrayObject.
Yeah, I know, this is why I said.
> That's exactly why we have to be very careful with strict types in dynamic
> language - because it makes excellent dynamic extensions line ArrayObject,
> __toString, etc. useless - since you can not really use one type instead of
> another as they intend, strict typing kills that.
> Coercive typing, on the other hand, would work just fine with those.
Agree.
>
>
> I mean it's too late to argue about that the current type hinting triggers
>> an error in case of type mismatch.
>>
>
> Actually we could change it if we wanted to, I don't believe there's really
> some code that relies on it being error for some functionality. The question
> is what we'd change it to, for which I have no good answer.
>
>
As I mentioned, I think that we have to inform the caller about the problem
(be either a type or a conversion mismatch), so the only options to trigger
an error, or throw an exception.
I like the exception idea better, because it can be easily handled localy
(no need to register a global error handler), but as the core devs said, it
isn't allowed to throw exceptions from the core, so this is why I think,
that the weak type hinting should be implemented as an spl interface.
This way we could even support array - ArrayObject conversion too.
http://wiki.php.net/rfc/splweaktypehintingwithautoboxing
The rfc name sucks, but I couldn't come up with a better one.
Tyrael