Both sides previous revisionPrevious revisionNext revision | Previous revision |
rfc:scalar-pseudo-type [2017/12/24 17:29] – Added more Examples fleshgrinder | rfc:scalar-pseudo-type [2017/12/24 23:33] (current) – Added object to the analogous list of other types fleshgrinder |
---|
| |
===== Proposal ===== | ===== Proposal ===== |
This RFC proposes a new ''scalar'' pseudo-type. This type is analogous to ''callable'' and ''iterable'', accepting multiple types instead of one single type. | This RFC proposes a new ''scalar'' pseudo-type. This type is analogous to ''callable'', ''iterable'', and ''object'', accepting multiple types instead of one single type. |
| |
''scalar'' accepts ''bool'', ''float'', ''int'', and ''string''. All of these types can be safely coerced to a ''string'' and be printed. | ''scalar'' accepts ''bool'', ''float'', ''int'', and ''string''. All of these types can be safely coerced to a ''string'' and be printed. |
| |
The function [[https://php.net/is-scalar|is_scalar]] to determine whether a value is ''scalar'' or not already exist in PHP since a long time and must not be added. | The function [[https://php.net/is-scalar|is_scalar]] to determine whether a value is ''scalar'' or not already exist in PHP since a long time and must not be added. |
| |
| ====== Weak Mode ====== |
| Objects with a magic ''toString'' method are accepted and treated as strings in weak mode. The behavior is 1:1 the same as if the type constraint would have been ''string'' in the first place for objects. This ensures perfect consistency and adheres to the principle of least astonishment. |
| |
====== Examples ====== | ====== Examples ====== |
===== Backward Incompatible Changes ===== | ===== Backward Incompatible Changes ===== |
''scalar'' is implemented as reserved name, therefore a class, interface, or trait named ''scalar'' cannot be declared. | ''scalar'' is implemented as reserved name, therefore a class, interface, or trait named ''scalar'' cannot be declared. |
| |
===== Open Issues ===== | |
* Whether ''scalar'' should accept objects with a magic ''toString'' method in weak mode like the ''string'' constraint does. | |
| |
===== Proposed PHP Version(s) ===== | ===== Proposed PHP Version(s) ===== |