Re: Type hinting
| From: | Zeev Suraski | Date: | Wed, 26 May 2010 21:20:24 +0000 |
| Subject: | Re: Type hinting | ||
| References: | 1 2 3 4 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-48478@lists.php.net to get a copy of this message | ||
At 23:44 26/05/2010, Davey Shafik wrote:
On May 26, 2010, at 3:08 PM, Zeev Suraski wrote: At 21:12 26/05/2010, Pierre Joye wrote:Yep - that's exactly the idea.I think that the proposed auto-converting type hints answer this request quite well... Zeev Do you propose to have a warning when the types are a mis-match, similar to the array->scalar conversion example from Gustavo? (strtoupper(array('ABC'))) If you have a warning or notice, that warns of potential loss of data accuracy, then I think I'd be OK with auto-converting.As PHP's type system is seen as a big plus, I have to say that many users consider it as a plus in the implementation of a given method function or method. But the same users ask to have something more strict for the methods signature. I think it is a valid request and somehow makes sense too.
Having said that, all other type hints are strict, throwing a catchable fatal error consistency for what is essentially the same syntax, is more important IMO.In my opinion it's really a matter of whether the value makes sense in its context or not. Much like a function that requires an object of type X will accept an object of type Y if Y extends from X - because Y is a kind of X, "42" is a kind of an int. It's true that no conversion is made in the former case, but still - across 99.9% of PHP, "42", 42 and 42.0 are the same thing.
Would it be possible to support two syntaxes:
function foo( (int) $bar) { } // auto-cast to int
function foo(int $bar) { } // require int
I would advise against it - IMHO having both strict and auto-converting type hints means having all of the issues of strict typing, and making that worse by having similar confusing syntaxes on top of it... In general I think there should be an exceptionally good reason to add core language syntax. Auto-converting type hints would cater for the vast majority of real world use cases. While I'm sure we can come up with situations where you'd want a strict type check, for those few cases - you could simply use is_int() - no need to add a language-level support for it.
Zeev