rfc:currying

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
Next revisionBoth sides next revision
rfc:currying [2011/06/05 23:35] – added PEP lstrojnyrfc:currying [2011/06/07 11:49] – More on error handling lstrojny
Line 1: Line 1:
 ====== Request for Comments: Currying ====== ====== Request for Comments: Currying ======
-  * Version: 0.9+  * Version: 0.9.1
   * Date: 2011-06-11   * Date: 2011-06-11
   * Author: Lars Strojny <lstrojny@php.net>   * Author: Lars Strojny <lstrojny@php.net>
Line 94: Line 94:
 $func("bar"); // foobaz $func("bar"); // foobaz
 </code> </code>
 +
 +=== Implementation details ===
 +Keyword ''curry'' would be replaced during parsing stage with an closure. A few examples how this transformation would work:
 +
 +<code php>
 +$func = curry strpos(..., "f");
 +</code>
 +
 +Would result in this code:
 +<code php>
 +$func = function($arg1) {
 +    return strpos($arg1, "f");
 +};
 +</code>
 +
 +Another example with a variable function name:
 +
 +<code php>
 +$funcName = "strpos";
 +$func = curry $funcName(..., "f");
 +</code>
 +
 +This would result in:
 +<code php>
 +$funcName = "strpos";
 +$func = function($arg1) use ($funcName) {
 +    return $funcName($arg1, "f");
 +};
 +</code>
 +
 +Example with variable arguments:
 +<code php>
 +$char = "f";
 +$func = curry strpos(..., $char);
 +</code>
 +
 +<code php>
 +$char = "f";
 +$func = function($arg1) use ($char) {
 +    return strpos($arg1, $char);
 +};
 +</code>
 +
 +== Pitfalls ==
 += There are a few concerns to implement it that way =
 +How is performance affected because of the heavy use of Closure objects? I don’t know yet, any guesses?
 +
 += Error Handling =
 +Error messages could be misleading. E.g. not passing an argument to ''$func()'' would result in a warning for a missing argument when calling ''$func()'' without any mention of ''strpos()''. One way to overcome this problem would be to have ''class CurriedFunction extends Closure''. This subclass would contain additional properties for a nicer the error message. It would even be possible to override error handling for ''CurriedFunction::__invoke()'' to make it more specific.
 +
 +  * Bad error message: ''Missing argument 1 for {closure}(), called in <file> on line <line> and defined in <file> on line <line>''
 +  * Better error message: ''Missing argument 1 for curried strpos(..., "f"), called in <file> on line <line> and defined in <file> on line <line>''
 +
 +Error handling for curried functions should be a little more strict in terms of "too many parameters". If somebody passed more parameters than defined in the curry statement, a warning should be thrown stating, that the additional parameters are ignored.
  
 ==== Notes  ==== ==== Notes  ====
-  * Keyword ''curry'' should have an alias ''schoenfinkel'' 
   * I could use some helping hand with the parser work, if you are interested, drop me a message   * I could use some helping hand with the parser work, if you are interested, drop me a message
  
 ==== References ==== ==== References ====
  
- Wikipedia: http://en.wikipedia.org/wiki/Currying +  * Wikipedia: http://en.wikipedia.org/wiki/Currying 
- PEP-0309: http://www.python.org/dev/peps/pep-0309/+  PEP-0309: http://www.python.org/dev/peps/pep-0309/
  
 ===== Changelog ===== ===== Changelog =====
-  * Initial draft+  * 0.9: Initial draft 
 +  * 0.9.1: RFC changed based on feedback from php-internals and #pecl.php 
 +    * Removed alias ''schoenfinkel'' :( 
 +    * Discussing implementation details and error handling
  
rfc/currying.txt · Last modified: 2017/09/22 13:28 by 127.0.0.1