Re: Renaming "strict types" to "scalar type coercion"

From: Date: Thu, 20 Jun 2024 21:39:11 +0000
Subject: Re: Renaming "strict types" to "scalar type coercion"
References: 1 2 3  Groups: php.internals 
Request: Send a blank email to [email protected] to get a copy of this message
On 19/06/2024 16:34, Larry Garfield wrote:
Also, as someone who does put every file into strict mode as a matter of course, and appreciates the many languages that do not even have a concept of non-strict mode (like Go or Rust), I really don't appreciate the backhanded comments in this thread about people who, you know, care about type safety. (Something that weak mode, when it was introduced in PHP 7, only marginally provided if at all.) Strict mode prevents bugs, full stop, and we should not fault anyone for applying that bug-stopper tool liberally.
Used correctly, it absolutely does. Used incorrectly, it can actually end up *hiding* errors. I've seen lots of cases where code like this: some_function_expecting_int($_GET['foo']); Gets changed to this: declare(strict_types=1); some_function_expecting_int( (int)$_GET['foo'] ); Even in PHP 7.0, the first version is actually *stricter* than the second, because explicit casts never fail, but parameter coercion (mode 0) always failed for strings like "hello"; as of 8.0, it also fails for strings like "123foo" and "". And this is exactly the kind of code that coercive type hints were intended for - HTTP is a text-based protocol, so most PHP applications are dealing with string input and output *all the time*. One of the things the language badly needs, and I have been trying to come up with a proposal for, is strict type casts, so that this code can actually be written safely but still concisely. Regards, -- Rowan Tommins [IMSoP]

Thread (28 messages)

« previous php.internals (#123714) next »