Re: What do we need strict scalar type hints for?

From: Date: Wed, 04 Feb 2015 18:12:14 +0000
Subject: Re: What do we need strict scalar type hints for?
References: 1 2 3 4 5 6 7 8  Groups: php.internals 
Request: Send a blank email to internals+get-81830@lists.php.net to get a copy of this message
Hi Robert, > On 4 Feb 2015, at 09:38, Robert Stoll <php@tutteli.ch> wrote: > > Personally I am not in favour of the declare approach because the user will need to switch > between strict context and non-strict context which will be quite challenging, anti-efficient (in > terms of productivity) and most probably another source for bugs. I’ve found this not to be quite true in practice. When I ported my app to use strict scalar hints, very little broke - and this is an app that took advantage of weak typing everywhere. I realise switching contexts might be a little painful, but it doesn’t seem to be as bad as I expected. It looks to me like much PHP code is already fairly strict and consistent with its types. I think it’s mostly just edge cases where things break with strict types on. Any strict code will work fine as “weak” code, and most weak code will work fine as strict code, I think. > Yet, I have to defend Andrea's RFC here since the claims made that it will break all kind > of existing code is just not true (or I got the RFC completely wrong). > > sin(1) would only be broken if and only if the user has decided to use it in a strict mode. > Consider the following: > > User A writes the file index.php > > <?php > $a = sin(1); //perfectly fine > include 'test_strict.php' > ?> > > and user B writes test_strict.php > > <?php > declare(strict=1); > $b = sin(1); //will fail and that's ok [*] since the user decided to be in strict mode > ?> > > As far as I can tell user A which want to code in non-strict mode could do so also in the > future - no error is reported, no problem at all. Only user B would get the error and only because > he/she wanted to be informed when his/her code is not strict. > > [*] we could actually distinguish between int literals/constants and int variables. The above > case would be fine as well since 1 can be int or float (pretty much as Haskell is handling it) Yeah, I only had int/float issues when I turned on strict mode in my app. And that caught a bug, actually, so I was quite thankful for it. -- Andrea Faulds http://ajf.me/

« previous php.internals (#81830) next »