Re: [RFC] Scalar Type Hinting With Casts (re-opening)
| From: | Andrea Faulds | Date: | Mon, 14 Jul 2014 15:20:33 +0000 |
| Subject: | Re: [RFC] Scalar Type Hinting With Casts (re-opening) | ||
| References: | 1 2 3 4 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-75473@lists.php.net to get a copy of this message | ||
On 14 Jul 2014, at 15:17, Andrea Faulds <ajf@ajf.me> wrote:
>
> On 14 Jul 2014, at 15:03, Rowan Collins <rowan.collins@gmail.com> wrote:
>
>> Looking at the current table in the RFC, I'm not clear why NULL should pass as any
>> value, but not array. Could it not behave the same as the existing type hints, i.e. accepted only if
>> declared as a default?
>>
>> function foo(array $bar) { } foo(null); // ERROR
>> function foo(array $bar=null) { } foo(null); // OK, $bar == NULL
>> function foo(int $bar) { } foo(null); // ERROR
>> function foo(int $bar=null) { } foo(null); // OK, $bar == NULL
>
> I’m thinking this as well. I wonder if perhaps it should be casted by default, but if you
> make it explicitly nullable, it won’t cast if NULL is passed.
This has been done now, as it was what I wanted to do and Anthony’s original intention. It works
like this:
function foo(int $a) { var_dump($a); }
function bar(int $a = NULL) { var_dump($a); }
foo(NULL); // int(0)
bar(NULL); // NULL
--
Andrea Faulds
http://ajf.me/