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

From: Date: Mon, 14 Jul 2014 17:56:46 +0000
Subject: Re: [RFC] Scalar Type Hinting With Casts (re-opening)
References: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15  Groups: php.internals 
Request: Send a blank email to internals+get-75502@lists.php.net to get a copy of this message
On 14 Jul 2014, at 18:38, Rowan Collins <rowan.collins@gmail.com> wrote: > Andrea Faulds wrote (on 14/07/2014): >> On 14 Jul 2014, at 18:24, Rowan Collins <rowan.collins@gmail.com> wrote: >> >>> If the current type hints were only for objects, I would be OK with that, but I really >>> don't get why an array cast is so different from an int cast. >> I’m don’t think the result of casting to array is meaningful or lossless for anything > > Lossless, no. Meaningful, yes. > > As mentioned earlier, the behaviour of this statement is as useful as any other forced cast: > > foreach ( (array)$foo as $bar ) Actually, the more important point is that [1] and 1, while they can be losslessly converted to each other, are not *equivalent* values. [1] * 2 is an error, for example. On the other hand, “1” * 2 works perfectly. Sure, the non-scalar types *can* be casted back and forth with scalar types, but you get a different kind of value that can’t be used in the same contexts. While scalar types can be and are regularly casted between each other, they are not regularly casted to and from non-scalar types, nor does it make sense. Arguably it’s inconsistent for the scalar and non-scalar type hints to work slightly differently, but the point is that we’d be doing what makes sense for arrays (no meaningful casts, so we don’t casts) and what makes sense for the scalar types (there are common, sensible, meaningful, lossless and equivalent casts, so we’ll do them). That’s my final word on this matter. -- Andrea Faulds http://ajf.me/

« previous php.internals (#75502) next »