rfc:allow_casting_closures_into_single-method_interface_implementations

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:allow_casting_closures_into_single-method_interface_implementations [2023/04/25 12:12] nicolasgrekasrfc:allow_casting_closures_into_single-method_interface_implementations [2023/04/25 19:36] (current) nicolasgrekas
Line 87: Line 87:
 ==== Example 2: Type-safe alternative to using Closure ==== ==== Example 2: Type-safe alternative to using Closure ====
  
-Imagine that you have the following function, that takes a closure and two number to do some operation on them:+Imagine that you have the following function, that takes a closure and two numbers to do some operation on them:
  
 <code php> <code php>
-function executeOperation(Closure $operation, int $a, int $b): int { +function executeOperation(Closure $operator, int $a, int $b): int { 
-    return $operation($a, $b);+    return $operator($a, $b);
 } }
 </code> </code>
Line 102: Line 102:
 } }
  
-function executeOperation(InvokableInterface $operation, int $a, int $b): int { +function executeOperation(OperatorInterface $operator, int $a, int $b): int { 
-    return $operation($a, $b);+    return $operator($a, $b);
 } }
  
Line 168: Line 168:
  
 In this example, an ''UriExpanderInterface'' is defined with an <php>expand()</php> method. In this example, an ''UriExpanderInterface'' is defined with an <php>expand()</php> method.
- 
 3 different implementations of the interface are derived using the <php>castTo()</php> method. 3 different implementations of the interface are derived using the <php>castTo()</php> method.
- 
 Finally, the <php>getExpandedUrl()</php> function is defined, which accepts an implementation of Finally, the <php>getExpandedUrl()</php> function is defined, which accepts an implementation of
 <php>UriExpanderInterface</php> and expands a URI template using the provided implementation. <php>UriExpanderInterface</php> and expands a URI template using the provided implementation.
rfc/allow_casting_closures_into_single-method_interface_implementations.1682424738.txt.gz · Last modified: 2023/04/25 12:12 by nicolasgrekas