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

From: Date: Tue, 15 Jul 2014 14:28:38 +0000
Subject: Re: [RFC] Scalar Type Hinting With Casts (re-opening)
References: 1 2 3 4 5  Groups: php.internals 
Request: Send a blank email to internals+get-75547@lists.php.net to get a copy of this message
Stas Malyshev wrote (on 14/07/2014):
But I'm not sure why you need this. You accept that if("foo") works. Then why "function foo(bool $x) { if($x) { ... " should work differently?
The answer - which is definitely a matter of opinion - is that allowing any string reduces the usefulness of the type hint. I realise there is not consensus on whether scalar hints should represent validation, cast, or a mixture, but *if* we go down the route of validation, then we have to choose which values are valid and which are not. My preference is to keep that strict: the example others have posted of some_func($foo & BIT_FLAG) seems no more like a "real" boolean to me than some_func(strlen($foo)) or any other expression which yields an integer. In fact, I'd find the behaviour more obvious if it were written some_func((bool)$foo & BIT_FLAG) - it makes clear that some_func is not itself aware of the flag, that's just the caller's way of making the decision. That the type hint encouraged that would therefore seem like a Good Thing. (I'd post that comment on the appropriate sub-thread where that example was raised, but don't have time to find it...) Regards, -- Rowan Collins [IMSoP]

Thread (250 messages)

« previous php.internals (#75547) next »