rfc:allow-closures-to-declare-interfaces-they-implement
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
rfc:allow-closures-to-declare-interfaces-they-implement [2023/04/20 16:21] – nicolasgrekas | rfc:allow-closures-to-declare-interfaces-they-implement [2023/04/25 12:15] (current) – nicolasgrekas | ||
---|---|---|---|
Line 3: | Line 3: | ||
* Version: 1.0 | * Version: 1.0 | ||
* Date: 2023-04-14 | * Date: 2023-04-14 | ||
- | * Author: Nicolas Grekas < | + | * Author: Nicolas Grekas < |
* Status: Draft | * Status: Draft | ||
* First Published at: https:// | * First Published at: https:// | ||
* Implementation: | * Implementation: | ||
- | ====== Introduction | + | ===== Introduction ===== |
This RFC proposes the addition of a syntax that allows closures to declare one or more interfaces they implement. This would provide developers with a more explicit and type-safe way to define and interact with closures in PHP. The proposed syntax is as follows: | This RFC proposes the addition of a syntax that allows closures to declare one or more interfaces they implement. This would provide developers with a more explicit and type-safe way to define and interact with closures in PHP. The proposed syntax is as follows: | ||
Line 16: | Line 16: | ||
</ | </ | ||
- | ====== Proposal | + | ===== Proposal ===== |
The current version of PHP allows closures to be used as a convenient way to define anonymous functions. However, the language does not offer a way to explicitly define the interfaces that a closure implements, which can lead to less clear and less type-safe code. The proposed change would allow closures to declare one or more interfaces they implement, providing a more robust and explicit way to interact with closures. | The current version of PHP allows closures to be used as a convenient way to define anonymous functions. However, the language does not offer a way to explicitly define the interfaces that a closure implements, which can lead to less clear and less type-safe code. The proposed change would allow closures to declare one or more interfaces they implement, providing a more robust and explicit way to interact with closures. | ||
- | Since closures implement only one '' | + | Since closures implement only one <php>__invoke()</ |
- | + | ||
- | ===== Syntax ===== | + | |
The proposed syntax adds an '' | The proposed syntax adds an '' | ||
Line 29: | Line 27: | ||
<code php> | <code php> | ||
- | $add = function (int $a, int $b) use ($c) implements AddInterface: int { | + | $add = function (int $a, int $b) use ($c): int implements AddInterface |
return $a + $b + $c; | return $a + $b + $c; | ||
}; | }; | ||
</ | </ | ||
- | ==== Example | + | ==== Example |
<code php> | <code php> | ||
- | $process = function ($input) use ($config) implements ProcessorInterface, | + | $process = function ($input) use ($config): Result |
// ... | // ... | ||
}; | }; | ||
</ | </ | ||
- | ==== Example | + | ==== Example |
<code php> | <code php> | ||
- | $square = fn (int $x) implements SquarerInterface: int => $x * $x; | + | $square = fn (int $x): int implements SquarerInterface |
</ | </ | ||
- | ====== | + | ==== Effect on reflection |
+ | |||
+ | Technically, | ||
+ | |||
+ | Conceptually: | ||
+ | <code php> | ||
+ | $f = function ($a) implements FooInterface { ... }; | ||
+ | </ | ||
+ | |||
+ | Should be equivalent to: | ||
+ | <code php> | ||
+ | $f = new class() extends Closure implements FooInterface { | ||
+ | public function __invoke($a) { | ||
+ | ... | ||
+ | } | ||
+ | }; | ||
+ | </ | ||
+ | |||
+ | This RFC does //not// propose to make this code valid. | ||
+ | |||
+ | This snippet is here to illustrate the possible side-effects of adding an interface to a closure as far as reflection is concerned. | ||
+ | |||
+ | ===== Benefits | ||
Introducing the ability to declare interfaces for closures would provide several benefits: | Introducing the ability to declare interfaces for closures would provide several benefits: | ||
- | * **Improved code readability**: | ||
* **Enhanced type safety**: By allowing closures to declare interfaces, type hinting can be used more effectively in function and method signatures that accept closures. This can help prevent runtime errors and improve overall code quality. | * **Enhanced type safety**: By allowing closures to declare interfaces, type hinting can be used more effectively in function and method signatures that accept closures. This can help prevent runtime errors and improve overall code quality. | ||
+ | * **Improved code readability**: | ||
* **Better IDE support**: With the addition of interface declarations for closures, IDEs can provide better code completion, error checking, and refactoring support. | * **Better IDE support**: With the addition of interface declarations for closures, IDEs can provide better code completion, error checking, and refactoring support. | ||
* **Promotes code reusability**: | * **Promotes code reusability**: | ||
Line 94: | Line 114: | ||
</ | </ | ||
- | ====== Backward Compatibility | + | ===== Backward Compatibility ===== |
The proposed change would be fully backward compatible, as it adds a new feature without affecting existing functionality. | The proposed change would be fully backward compatible, as it adds a new feature without affecting existing functionality. | ||
- | ====== Future Scope ====== | + | ===== Future Scope ===== |
* The base Closure class could be extended to add a '' | * The base Closure class could be extended to add a '' | ||
* Closures could be auto-cast to compatible interfaces when possible. See https:// | * Closures could be auto-cast to compatible interfaces when possible. See https:// | ||
- | ==== Proposed PHP Version ==== | + | ===== Proposed PHP Version |
This RFC targets PHP version 8.3. | This RFC targets PHP version 8.3. | ||
- | ==== Vote ==== | + | ===== Vote ===== |
The vote will require a 2/3 majority to be accepted. Voting will start on [Vote start date] and end on [Vote end date]. | The vote will require a 2/3 majority to be accepted. Voting will start on [Vote start date] and end on [Vote end date]. | ||
rfc/allow-closures-to-declare-interfaces-they-implement.1682007708.txt.gz · Last modified: 2023/04/20 16:21 by nicolasgrekas