Re: [RFC] Scalar Type Hinting With Casts (re-opening)

From: Date: Sun, 13 Jul 2014 15:29:42 +0000
Subject: Re: [RFC] Scalar Type Hinting With Casts (re-opening)
References: 1 2 3 4 5 6 7 8  Groups: php.internals 
Request: Send a blank email to internals+get-75417@lists.php.net to get a copy of this message
On 13 Jul, 2014, at 11:17 pm, Rowan Collins <rowan.collins@gmail.com> wrote: > On 13/07/2014 15:59, Jocelyn Fournier wrote: >> From my point of view, if the type annotations are doing implicit cast (with or without >> E_NOTICE/E_STRICT warning), they should behave exactly the same than an explicit cast. If it behaves >> differently, I'll be really difficult for a developer to guess what will be the PHP behaviour >> with this new syntax. > > The problem is, in PHP an explicit type cast never fails - (int)'abc' simply gives > you the value 0, not an error. If you let scalar typehints just generate unchecked casts, we'd > have this: > > wants_object(object $foo) { var_dump($foo); } > wants_int(int $foo) { var_dump($foo); } > > wants_object('abc'); // fails with E_RECOVERABLE_ERROR > wants_int('abc'); // succeeds and prints int(0) Depending on the final implementation, the latter may raise a notice about information loss. > > > That seems both inconsistent and less useful than a hybrid juggling + validation approach. While this is indeed inconsistent, scalars and objects aren’t the same thing, though, and I think it’s okay to treat them differently. > > -- > Rowan Collins > [IMSoP] > > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php >

« previous php.internals (#75417) next »