On 03/06/2016 13:30, Bob Weinand wrote:

The *weak casting* rules (i.e. what gets converted how and what can be
converted to the other type) are not an invention of the proposal.

The proposal however defines the specific order.

I’ve added a reasoning why not left-to-right now. As this seemed to be
asked multiple times.

Also, the specific reasoning why I propose that specific order the rules
are in:
Otherwise PHP's casting rules are applied in an order to be as
lossless as possible.

That’s really the only motivation behind that.


Cheers, that's more or less what I thought.

I've yet to come up with a scheme that can be summarised any more simply than that table, but FWIW below is how I've been trying to think about it...




Strict mode rules are easy; try, in order:
0. Match without casting
1. Cast int -> float

Weak mode rules must start with the same rules strict mode would, to avoid confusion.

After that, there are various casts available, and there's not a clear precedent for the order that I can think of.

There are two somewhat contradictory aims:
- use "narrower" types where possible, e.g. for string("") passed to "int | bool" we probably prefer bool(false) over int(0) - use lossless casts where possible, e.g. for float(1.5) passed to "int | string" we probably prefer string("1.5") over int(1)

The following casts are always lossless:

- anything -> string (with possible exception of float precision limits)
- boolean -> anything (1 bit of information is easy to retain :P)

The following may be lossless in some common cases:

- string -> int (ctype_digit)
- string -> float (roughly, is_numeric)
- float -> int (whole numbers)
- int -> float (where magnitude < 2^53, but this is pre-empted as high priority by the strict rules)

The following are pretty much always lossy:

- anything -> boolean




I'm not quite sure what to conclude from all that, but thought I'd write it down for the record.

Cheers,
--
Rowan Collins
[IMSoP]

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to