rfc:typesafe-callable

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
rfc:typesafe-callable [2016/04/04 22:09]
dasprid created
rfc:typesafe-callable [2017/09/22 13:28] (current)
Line 9: Line 9:
 Currently, when you typehint against "​callable"​ in your method signatures, you can never be certain whether it accepts the parameters you are giving it or if the return type is what you'd expect. You can of course validate the return type, and catch exceptions about the invalid parameters and such when calling the callable, but that is not ideal. Currently, when you typehint against "​callable"​ in your method signatures, you can never be certain whether it accepts the parameters you are giving it or if the return type is what you'd expect. You can of course validate the return type, and catch exceptions about the invalid parameters and such when calling the callable, but that is not ideal.
  
-This RFC tries to solve this problem by introducing ​implicit interface for callables.+This RFC tries to solve this problem by introducing ​user-defined ​callables.
  
 ===== Proposal ===== ===== Proposal =====
-This RFC proposes ​to allow creating special interfaces which extend ​the "​callable" ​type. A simple ​example ​could look like this:+This RFC proposes the definition of user-defined callables. Since "​callable" ​is already a reserved keyword, this should not be a BC break. User-defined callables should be be both allowed to be namespaced like functions and classes, but also invoke the autoloader if they are not found in the current scope. A simple ​callable definition ​could look like this:
  
 <code php> <code php>
-interface MyCallable extends ​callable +callable ​FilterCallable(string $input) : string;
-+
-    public function __invoke(string $foo) : int; +
-}+
 </​code>​ </​code>​
  
-Now any function or method could typehint against ​MyCallable instead of callable ​and be certain that the parameters ​are accepted and the correct ​type is returned:+With such a callable in place, the user can now typehint against ​it, like they would against any other property, ​and be certain that it will accept ​the parameters ​they pass in, as well as guarantee ​the return ​type they expect:
  
 <code php> <code php>
-function foo(MyCallable ​$foo) : int+function foo(FilterCallable ​$filter) : string
 { {
-    return $foo('​bar'​);​+    return $filter('​bar'​);​
 } }
  
-foo(function (string $foo) { return ​strlen($foo) });+foo(function (string $foo) : string ​{ return ​trim($foo)});
 </​code>​ </​code>​
  
-Of course, this works with any other kind of callable as well, be it a string, an array or an object. Even with the object, it would not have to implement the interface, since it is considered a special one due to the extension of the "​callable"​ type. +Of course, this works with any other kind of callable as well, be it a string, an array or an object.
- +
-Since we do have an interface though, which technically represents a class, any callable would have to allow to be also invoked by calling $callable->​__invoke().+
  
 ===== Backward Incompatible Changes ===== ===== Backward Incompatible Changes =====
rfc/typesafe-callable.1459807766.txt.gz · Last modified: 2017/09/22 13:28 (external edit)