Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision |
rfc:variable_syntax_tweaks [2020/01/07 15:58] – Fix email link girgias | rfc:variable_syntax_tweaks [2020/01/28 16:21] – nikic |
---|
* Author: Nikita Popov <nikic@php.net> | * Author: Nikita Popov <nikic@php.net> |
* Target Version: 8.0 | * Target Version: 8.0 |
* Status: Under Discussion | * Status: Voting |
* Implementation: https://github.com/php/php-src/pull/5061 | * Implementation: https://github.com/php/php-src/pull/5061 |
| |
==== Arbitrary expression support for new and instanceof ==== | ==== Arbitrary expression support for new and instanceof ==== |
| |
Most syntactic constructions that normally require an identifier also accept a syntax variation that accepts an arbitrary expression. For the most part, this uses curly braces, such as in ''${expr}'', ''%%$foo->{expr}%%'', ''FOO::{expr}()'', etc. On the left-hand-side of an expression, parentheses are used instead, such as in ''(expr)::foo()''. | Most syntactic constructions that normally require an identifier/name also accept a syntax variation that allows an arbitrary expression. Depending on the case, this uses either ''{expr}'' or ''(expr)'' syntax. Places that accept a simple (non-namespaced) identifier use curly braces, such as ''${expr}'', ''%%$foo->{expr}%%'', and ''FOO::{expr}()''. Places that accept a namespaced name use parentheses, such as ''(expr)()'' and ''(expr)::FOO''. These cases also accept more than just strings, in particular ''(expr)()'' additionally also allows other callables, and ''(expr)::FOO'' also allows objects. |
| |
One place where arbitrary expressions are currently not supported are class names in ''new'' and ''instanceof'' (which are currently treated as syntactically same). | One place where arbitrary expressions are currently not supported are class names in ''new'' and ''instanceof'' (which are currently treated as syntactically the same). |
| |
This RFC proposes to introduce the syntax ''new {expr}'' and ''$x instanceof {expr}'' respectively. | In line with the considerations above, this RFC proposes to introduce the syntax ''new (expr)'' and ''$x instanceof (expr)'' respectively. |
| |
=== Instanceof alternatives === | ===== Backward Incompatible Changes ===== |
| |
While for ''new'' there is just one possible syntax choice that is consistent with the overall syntax design, there are two additional possibilities for ''instanceof'': One is to instead use ''$x instanceof (expr)'', which may make more sense, as ''instanceof'' essentially looks like a normal binary operator. | There are not backwards incompatible changes, this RFC allows strictly more syntax than before. |
| |
The second possibility is to relax the restrictions around the right-hand-side of ''instanceof'' entirely. This would involve treating it as a normal expression, and then reinterpreting plain constant accesses as class name references instead. I think that in principle this is a good option, but am concerned that it will cause issues in the future if we introduce generic type support, in which case a class name will no longer necessarily coincide with an ordinary expression. | ===== Future Scope ===== |
| |
===== Backward Incompatible Changes ===== | It is in principle possible to relax the limitations on the right-hand-side of ''instanceof'' entirely: It could be treated as a normal expression, with plain constant accesses being reinterpreted as class name references instead (as this is the only ambiguity between them). While I think that this is the best option in principle, I am concerned that it will cause issues in the future if we introduce generic types, in which case a class name will no longer necessarily coincide with an ordinary expression. |
| |
There are not backwards incompatible changes, this RFC allows strictly more syntax than before. | |
| |
===== Vote ===== | ===== Vote ===== |
| |
Unless there are concerns regarding specific parts of the RFC, a monolithic vote will be used. | Voting started 2020-01-28 and ends 2020-02-11. |
| |
| <doodle title="Implement syntax changes outlined above in PHP 8?" auth="nikic" voteType="single" closed="false"> |
| * Yes |
| * No |
| </doodle> |