rfc:functional-interfaces

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:functional-interfaces [2016/04/17 19:20] krakjoerfc:functional-interfaces [2017/09/22 13:28] (current) – external edit 127.0.0.1
Line 1: Line 1:
 ====== PHP RFC: Functional Interfaces ====== ====== PHP RFC: Functional Interfaces ======
   * Version: 0.1   * Version: 0.1
-  * Date: 2016-04-17 +  * Date: 2016-04-17
   * Author: krakjoe   * Author: krakjoe
-  * Status: Draft+  * Status: Declined
   * First Published at: http://wiki.php.net/rfc/functional-interfaces   * First Published at: http://wiki.php.net/rfc/functional-interfaces
  
Line 35: Line 35:
 </code> </code>
  
-There is enough information in the code above for the compiler to reason that $cb should implement IFoo, and obviously be a Closure.+There is enough information in the code above for the engine to reason that $cb should implement IFoo, and obviously be a Closure.
  
 The engine generates the appropriate class entry using the closure as the only public method, having easily determined the correct name for that method (there is, and can only be, one possible candidate). The engine generates the appropriate class entry using the closure as the only public method, having easily determined the correct name for that method (there is, and can only be, one possible candidate).
Line 43: Line 43:
 The code below is not good code, it's not the most efficient version of the code that could exist. It serves to show the difference between implementing a functional interface using a closure, and provides a comparison with anonymous classes: The code below is not good code, it's not the most efficient version of the code that could exist. It serves to show the difference between implementing a functional interface using a closure, and provides a comparison with anonymous classes:
  
-[[https://3v4l.org/b4AWq/rfc#tabs|Functional Interfaces - Counter Example]]+[[https://3v4l.org/b4AWq/rfc#rfc-func_interfaces|Functional Interfaces - Counter Example]]
  
 <code php> <code php>
Line 180: Line 180:
   * supports lexical scope   * supports lexical scope
  
-Functional interfaces don'change the definition of an interface and only reuse the definition of a Closure.+Functional interface support does not change the definition of an interfaceand only reuse the definition of a Closure.
  
 ===== Receiving and Invoking Functional Interfaces ===== ===== Receiving and Invoking Functional Interfaces =====
Line 188: Line 188:
 The implementation would have the following formal definition: The implementation would have the following formal definition:
  
-<code> +<code>final class {Interface}\0{closure} extends Closure implements Interface</code>
-class {Interface}\0{closure} extends Closure implements Interface +
-</code>+
  
 Such that the following is always true: Such that the following is always true:
  
-''$instance instanceof IFoo && $instance instanceof Closure''+<code>$instance instanceof Interface && $instance instanceof Closure</code>
  
-[[https://3v4l.org/mOJXI/rfc#tabs|Functional Interfaces - Receiving and Invoking]]+[[https://3v4l.org/mOJXI/rfc#rfc-func_interfaces|Functional Interfaces - Receiving and Invoking]]
  
 <code php> <code php>
Line 224: Line 222:
 </code> </code>
  
-This means that the receiver (Foo::__construct) can receive, and consumer (Foo::thing) can invoke the interface as if it were a normal object, while the creator of $logger, who must know it is a Closure, can still invoke it as a Closure.+This means that the receiver (%%Foo::__construct%%) can receive, and consumer (Foo::thing) can invoke the interface as if it were a normal object, while the creator of $logger, who must know it is a Closure, can still invoke it as a Closure.
  
 Both methods of invocation are valid in both receiving and declaring contexts. Both methods of invocation are valid in both receiving and declaring contexts.
Line 231: Line 229:
 The following conditions will cause compiler errors: The following conditions will cause compiler errors:
  
-[[https://3v4l.org/65W6i/rfc#tabs|Functional Interfaces - Compiler Error 1]]+[[https://3v4l.org/65W6i/rfc#rfc-func_interfaces|Functional Interfaces - Compiler Error 1]]
  
 <code php> <code php>
Line 248: Line 246:
 //Reason: IFoo cannot be considered a functional interface, because it contains more than one abstract method.// //Reason: IFoo cannot be considered a functional interface, because it contains more than one abstract method.//
  
-[[https://3v4l.org/qLbPv/rfc#tabs|Functional Interfaces - Compiler Error 2]]+[[https://3v4l.org/qLbPv/rfc#rfc-func_interfaces|Functional Interfaces - Compiler Error 2]]
  
 <code php> <code php>
Line 266: Line 264:
 //Reason: Although IBar only declares one abstract method, it extends IFoo and so contains two abstract methods// //Reason: Although IBar only declares one abstract method, it extends IFoo and so contains two abstract methods//
  
-[[https://3v4l.org/WT98N/rfc#tabs|Functional Interfaces - Compiler Error 3]]+[[https://3v4l.org/WT98N/rfc#rfc-func_interfaces|Functional Interfaces - Compiler Error 3]]
  
 <code php> <code php>
Line 281: Line 279:
 //Reason: Although Foo contains only one abstract method, it is not an interface// //Reason: Although Foo contains only one abstract method, it is not an interface//
  
-[[https://3v4l.org/MMuD0/rfc#tabs|Functional Interfaces - Compiler Error 4]]+[[https://3v4l.org/MMuD0/rfc#rfc-func_interfaces|Functional Interfaces - Compiler Error 4]]
  
 <code php> <code php>
-... +<?php 
-    function () implements self {}; +new class { 
-...+    public function __construct() { 
 +        function () implements self {}; 
 +    } 
 +};
 </code> </code>
- 
-**See 3v4l for this example, the wiki has problems displaying the code.** 
  
 <code>Fatal error: functional interface cannot implement self in /in/MMuD0 on line 4</code> <code>Fatal error: functional interface cannot implement self in /in/MMuD0 on line 4</code>
Line 295: Line 294:
 //Reason: Although self is a valid scope in that context, self, parent, and static, can never be interfaces// //Reason: Although self is a valid scope in that context, self, parent, and static, can never be interfaces//
  
-[[https://3v4l.org/2AiUV/rfc#tabs|Functional Interfaces - Compiler Error 5]]+[[https://3v4l.org/2AiUV/rfc#rfc-func_interfaces|Functional Interfaces - Compiler Error 5]]
  
 <code php> <code php>
Line 310: Line 309:
 //Reason: The compiler would raise less specific errors later on// //Reason: The compiler would raise less specific errors later on//
  
-[[https://3v4l.org/o9gIB/rfc#tabs|Functional Interfaces - Compiler Error 6]]+[[https://3v4l.org/o9gIB/rfc#rfc-func_interfaces|Functional Interfaces - Compiler Error 6]]
  
 <code php> <code php>
Line 324: Line 323:
  
 //Reason: The compiler would raise less specific errors later on// //Reason: The compiler would raise less specific errors later on//
 +
 +===== Syntax Choices =====
 +
 +Interface and return type reversed:
 +
 +<code>function (string $arg) use($thing) : int implements Interface</code>
 +
 +It looks as if ''int'' is somehow implementing ''Interface''.
 +
 +Interface before arguments:
 +
 +<code>function implements Interface (string $arg) use($thing) : int {}</code>
 +
 +The arguments list looks as if it somehow applies to ''Interface''.
 +
 +Interface after arguments and before use:
 +
 +<code>function (string $arg) implements Interface use($thing) : int {}</code>
 +
 +This looks as if ''Interface'' somehow uses ''$thing''.
 +
 +===== Vote =====
 +
 +Voting started on May 15th, ended May 29th 2016.
 +
 +<doodle title="Accept functional interfaces? (2/3+1 majority required)" auth="krakjoe" voteType="single" closed="true">
 +   * Yes
 +   * No
 +</doodle>
  
 ===== Backward Incompatible Changes ===== ===== Backward Incompatible Changes =====
Line 334: Line 362:
  
 ==== To Existing Extensions ==== ==== To Existing Extensions ====
-Extensions will be able to provide implementations of functional interfaces as part of their API.+The API to create functional interface implementations is exported by Zend, and part of the already exported Closure API.
  
 ==== To Opcache ==== ==== To Opcache ====
 Opcache may need a trivial patching. Opcache may need a trivial patching.
- 
-===== Open Issues ===== 
-TBD 
  
 ===== Future Scope ===== ===== Future Scope =====
-This sections details areas where the feature might be improved in futurebut that are not currently proposed in this RFC.+When the concept of functional interfaces is implemented, it may be worth discussing the coercionor explicit cast of callables.
  
 ===== Proposed Voting Choices ===== ===== Proposed Voting Choices =====
Line 357: Line 382:
  
 ===== References ===== ===== References =====
-Links to external references, discussions or RFCs+ 
 +[[http://news.php.net/php.internals/92404|php.internals]]
rfc/functional-interfaces.1460920841.txt.gz · Last modified: 2017/09/22 13:28 (external edit)