rfc:instanceof_improvements

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
Next revisionBoth sides next revision
rfc:instanceof_improvements [2021/03/08 16:04] – ++ maxsemrfc:instanceof_improvements [2021/03/09 09:51] maxsem
Line 7: Line 7:
  
 ===== Introduction ===== ===== Introduction =====
-As currently implemented, the `instanceofoperator is inconsistent.+As currently implemented, the ''instanceof'' operator is inconsistent.
 <code php> <code php>
 var_dump(new MyClass instanceof MyClass); // true var_dump(new MyClass instanceof MyClass); // true
Line 20: Line 20:
 Make ''instanceof'' support non-object types: Make ''instanceof'' support non-object types:
 <code php> <code php>
-var_dump('foo' instanceof string)       // true +var_dump('foo' instanceof string);      // true 
-var_dump('foo' instanceof ('string'))   // true+var_dump('foo' instanceof ('string'));  // true
 $type = 'string'; $type = 'string';
 var_dump('foo' instanceof $type);       // true var_dump('foo' instanceof $type);       // true
 </code> </code>
 +
 +==== Types to support ====
 +This proposal covers only concrete scalar types ''int'', ''float'', ''string'', ''bool'' and ''null''; as well as compound types ''array'', ''object'', ''callable'' and ''iterable''. Other types are intentionally omitted:
 +  * ''mixed'' is pointless because there are easier ways to produce an expression always evaluating to true than ''$something instanceof mixed''.
 +  * ''void'' is kinda obvious, but I'm mentioning it just to be thorough.
 +
 +All attempts to check against these types would evaluate to ''false'' (just as currently) and produce warnings in 8.1, upgraded to fatals in 8.2.
 +
 +  * ''resource'' is not available as a parameter type and is on its way out, so it will be unaffected by this RFC and treated as a class name, available for userspace to use.
 +
 +==== Legacy type aliases ====
 +There are several legacy types, supported only for typecasts: ''(integer)'', ''(double)'', ''(boolean)'' and ''(binary)''. They have never worked for any other situations: e.g. the first parameter of ''function f(integer $x)'' is interpreted as a class called ''integer'', we even started issuing warnings in such situations since 8.0. I propose to extend this kind of treatment to ''instanceof'' too, issuing the same warning.
 +
 +==== Constant expressions on the left hand side ====
 +The current implementation has a shortcut where if there is a constant expression to the left of ''instanceof'', the result is hardcoded as false, since the operator supports only class names on the right and constant expressions can't produce objects. This raises a question: how should be Captain Obvious cases like ''123 instanceof int'' be treated? What about slightly less obvious cases like ''123 instanceof $typeName''? For comparison, I've checked two languages that have approaches to OOP similar to PHP:
 +  * In Java, a variable is required on the left side of ''instanceof''. All types of expressions are disallowed.
 +  * In C#, constant expressions to the left of ''is'' produce a correct result but a compiler warning is issued.
 +  * None of these support type names as strings, so the latter use case has no direct analogs.
 +
 +I can see two possibilities why such constructs might appear in code:
 +  * An clueless developer trying to achieve with ''123 instanceof $type'' something for which ''$type === "int"'' is a better solution.
 +  * A code generator went astray and generates something dubious.
 +Considering this, I don't think that adding support for constant expressions on LHS would do our end users any good. I propose to continue shortcutting such cases to false and additionally let the developers know they're doing something wrong with ''E_COMPILE_WARNING''.
  
 ===== Backward Incompatible Changes ===== ===== Backward Incompatible Changes =====
rfc/instanceof_improvements.txt · Last modified: 2022/04/18 10:51 by ilutov