On Sat, 6 Feb 2021 at 00:46, David Gebler <davidgeb...@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

Reply via email to