Re: [RFC] Warning for implicit float to int conversions

From: Date: Sat, 06 Feb 2021 00:17:29 +0000
Subject: Re: [RFC] Warning for implicit float to int conversions
References: 1 2 3 4 5  Groups: php.internals 
Request: Send a blank email to internals+get-113097@lists.php.net to get a copy of this message
On Sat, 6 Feb 2021 at 00:46, David Gebler <davidgebler@gmail.com> wrote: > Generating a warning on explicit casts of (non-integer) floats to int > would IMO make no sense at all, it would put PHP at odds with other major > languages such as C, Python and Java and go against normal, reasonable > expectations of how a programming language behaves. > > You said in an earlier comment "it's better to be explicit about your > intent", but doing something like (int)3.5 *is* being explicit about your > intent - and truncating casts on float to int is the widely established > norm. > > This was exactly my reservation about deprecating this behaviour even as > an implicit cast - in my mind it isn't a bug or flaw, it's there by design. > > If developers want to round/ceil/floor/do whatever with a float prior to > using it as an int, they already have that option and the greatest > flexibility. > > At least with the implicit case, I understand the motivation and argument > for bringing coercion more in line with strict typing behaviour and > catching cases where such a cast may not have been intentional (though I > still think a warning is too high an error level for this and would favour > a notice or deprecation, were it to be done at all). > Let's agree to disagree on what would be the ideal behaviour of type casts. I do understand that it would be a big concern for BC if (int) stopped working for floats with a fractional part. Could we at least fix the odd cases where the cast is definitely a failure? Like: (int) 1e60; // 0 (int) "foo"; // 0 — Benjamin

« previous php.internals (#113097) next »