rfc:scalar-pseudo-type

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
rfc:scalar-pseudo-type [2017/12/24 16:59] – Added Examples fleshgrinderrfc:scalar-pseudo-type [2017/12/24 23:33] (current) – Added object to the analogous list of other types fleshgrinder
Line 10: Line 10:
  
 ===== 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.
Line 81: Line 81:
  
 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 ======
Line 93: Line 96:
  
 However, it is also useful in userland. However, it is also useful in userland.
 +
 +<code php>
 +interface Parser {
 +    /** @return static */
 +    function parse(scalar $input);
 +}
 +</code>
  
 <code php> <code php>
Line 123: Line 133:
  
 ======= Scalar Returns ======= ======= Scalar Returns =======
-The return type constraint is less commonly useful than the one for parameters, however, it is specifically of interest while designing supertypes for others and to work around the magic ''__toString'' method that can only return values of type ''string'' (and is incompatible with exceptions).+The return type constraint is less commonly useful than the one for parameters, however, it is specifically of interest while designing supertypes for others and to work around the magic ''toString'' method that can only return values of type ''string'' (and is incompatible with exceptions).
  
 <code php> <code php>
rfc/scalar-pseudo-type.1514134798.txt.gz · Last modified: 2017/12/24 16:59 by fleshgrinder