rfc:stricter_implicit_boolean_coercions
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
rfc:stricter_implicit_boolean_coercions [2022/05/29 08:56] – iquito | rfc:stricter_implicit_boolean_coercions [2022/06/20 16:00] (current) – Change status to declined and close voting iquito | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== PHP RFC: Stricter implicit boolean coercions ====== | ====== PHP RFC: Stricter implicit boolean coercions ====== | ||
- | * Version: 1.7 | + | * Version: 1.9 |
* Date: 2022-05-16 | * Date: 2022-05-16 | ||
* Author: Andreas Leathley, < | * Author: Andreas Leathley, < | ||
- | * Status: | + | * Status: |
* First Published at: http:// | * First Published at: http:// | ||
Line 10: | Line 10: | ||
When not using strict_types in PHP, scalar type coercions have become less lossy/ | When not using strict_types in PHP, scalar type coercions have become less lossy/ | ||
- | Some examples | + | Some examples |
<PHP> | <PHP> | ||
- | function toBool(bool $a) | + | function toBool(bool $a) { var_dump($a); |
- | { | + | |
- | | + | |
- | } | + | |
toBool(' | toBool(' | ||
Line 67: | Line 64: | ||
This RFC boils down to these questions: | This RFC boils down to these questions: | ||
- | * Are you losing information when you reduce a value like -375, " | + | * Are you losing information when you reduce a value like -375, " |
- | * Would you want to know when a value like -375, " | + | * Would you want to know when a value like -375, " |
* How likely is it that such a coercion is unintended? | * How likely is it that such a coercion is unintended? | ||
- | * What about other boolean coercions in PHP? (this is handled in the next section) | + | * What about other boolean coercions in PHP? (handled in the [[stricter_implicit_boolean_coercions# |
+ | * How does this RFC fit in with the existing scalar type coercions? (handled in the [[stricter_implicit_boolean_coercions# | ||
The main motivation for this RFC is to reduce the possibility of errors when using the boolean type in a similar way that you cannot give a typed int a non-number string - if you provide " | The main motivation for this RFC is to reduce the possibility of errors when using the boolean type in a similar way that you cannot give a typed int a non-number string - if you provide " | ||
Line 91: | Line 89: | ||
Typed booleans (arguments, returns, properties) as discussed in this RFC are not the only part of PHP where implicit boolean coercions happen. They also occur in expressions like '' | Typed booleans (arguments, returns, properties) as discussed in this RFC are not the only part of PHP where implicit boolean coercions happen. They also occur in expressions like '' | ||
- | However | + | Using strict_types is an established way to change how scalar type coercions work (by prohibiting any coercions) but it does not affect implicit boolean coercions |
<PHP> | <PHP> | ||
Line 112: | Line 110: | ||
</ | </ | ||
- | Typed booleans behave differently compared to these expressions because they do not accept arrays, resources, objects and null. Further restricting typed booleans is therefore not a change which makes the language more inconsistent, | + | Typed booleans behave differently compared to boolean |
<PHP> | <PHP> | ||
Line 154: | Line 152: | ||
===== Overview of scalar type coercions ===== | ===== Overview of scalar type coercions ===== | ||
- | This is the status of coercions if this RFC passes (and any deprecation notices and TypeErrors are avoided) - the new behavior can be seen in the last column (To bool): | + | This is the status of coercions if this RFC passes (and any deprecation notices and TypeErrors are avoided) - the new behavior can be seen in the last column (To bool), it is quite symmetrical to the existing behavior in the "From bool" row: |
| ^ To string | | ^ To string | ||
- | ^ From string | + | ^ From string |
- | ^ From int | always possible | + | ^ From int | always possible |
- | ^ From float | always possible | + | ^ From float | always possible |
- | ^ From bool | always possible (coerced to "" | + | ^ From bool | always possible (coerced to "" |
- | This RFC would further reduce the gap between strict mode and coercive mode, as even in coercive mode no information would be lost and only values that are reasonable are accepted (or a deprecation notice | + | This RFC would further reduce the gap between strict mode and coercive mode, as even in coercive mode no information would be lost when coercing a scalar value and only values that are reasonable are accepted (otherwise |
+ | |||
+ | < | ||
+ | function toBool(bool $a) { return $a; } | ||
+ | function toString(string $a) { return $a; } | ||
+ | function toInt(int $a) { return $a; } | ||
+ | function toFloat(float $a) { return $a; } | ||
+ | |||
+ | toString(toBool('' | ||
+ | toInt(toBool(0)); | ||
+ | toFloat(toBool(0.0)); | ||
+ | |||
+ | toString(toBool(' | ||
+ | // => ' | ||
+ | // the new deprecation notice of this RFC points | ||
+ | |||
+ | toInt(toBool(-33)); | ||
+ | // => -33 is coerced to true and then back to 1 | ||
+ | // the new deprecation notice of this RFC points out the loss of information | ||
+ | |||
+ | toFloat(toBool(0.01)); | ||
+ | // => 0.01 is coerced to true and then back to 1 | ||
+ | // the new deprecation notice of this RFC points out the loss of information | ||
+ | |||
+ | // Existing behavior leading to TypeErrors and deprecation notices: | ||
+ | toFloat(' | ||
+ | toInt(' | ||
+ | toString(['' | ||
+ | toBool(null); | ||
+ | </ | ||
+ | |||
+ | Having as little information loss as possible when coercing scalar types makes them safer to use and more predictable. | ||
===== Implementation notes ===== | ===== Implementation notes ===== | ||
Line 194: | Line 223: | ||
</ | </ | ||
- | With the many deprecation notices that appeared in PHP 8.0 and 8.1 there is some wariness if more deprecation notices are worth it. These are the arguments why the RFC author thinks it will not cause too much pain: | + | With the many deprecation notices that appeared in PHP 8.0 and 8.1 there is some wariness if new deprecation notices are worth it. These are the arguments why the RFC author thinks it will not cause too much pain: |
* Each individual case is easy to fix, the easiest (but also least useful) is to loosly compare a value to true ($value == true) instead of directly giving the value to a typed bool | * Each individual case is easy to fix, the easiest (but also least useful) is to loosly compare a value to true ($value == true) instead of directly giving the value to a typed bool | ||
Line 203: | Line 232: | ||
===== Future Scope ===== | ===== Future Scope ===== | ||
- | While this RFC only targets boolean coercions when not using strict_types, | + | While this RFC only targets boolean coercions when not using strict_types, |
- | One benefit of these well-developed coercions could be to make them available in an explicit way to PHP developers. Having functions like '' | + | One benefit of these well-developed coercions could be to make them available in an explicit way to PHP developers. Having functions like '' |
- | If this RFC is accepted I would likely follow up with a discussion about a way to invoke the implicit coercion behavior in an explicit way, which would make it available even when using strict_types. | + | An example of how these functions could look like can be found on Github in [[https:// |
| | ||
===== Proposed PHP Version ===== | ===== Proposed PHP Version ===== | ||
Line 219: | Line 248: | ||
* Implicit boolean expressions (as used in if, ternary, logic operators) are not affected. | * Implicit boolean expressions (as used in if, ternary, logic operators) are not affected. | ||
* FILTER_VALIDATE_BOOLEAN in the filter extension is not affected. | * FILTER_VALIDATE_BOOLEAN in the filter extension is not affected. | ||
+ | | ||
+ | ===== Vote ===== | ||
+ | |||
+ | Voting started on 2022-06-06 and will end on 2022-06-20. | ||
+ | |||
+ | <doodle title=" | ||
+ | * Yes | ||
+ | * No | ||
+ | </ | ||
===== Patches and Tests ===== | ===== Patches and Tests ===== |
rfc/stricter_implicit_boolean_coercions.1653814601.txt.gz · Last modified: 2022/05/29 08:56 by iquito