rfc:short-syntax-for-anonymus-functions

Differences

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

Link to this comparison view

Next revision
Previous revision
rfc:short-syntax-for-anonymus-functions [2013/02/19 12:41] – created mdrfc:short-syntax-for-anonymus-functions [2017/09/22 13:28] (current) – external edit 127.0.0.1
Line 1: Line 1:
-====== Request for Comments: Short syntax for anonymus functions ====== +===== This topic does not exist yet =====
-  * Version: 1.0 +
-  * Date: 2008-03-06 +
-  * Author: Marcello Duarte <marcello.duarte@gmail.com> +
-  * Status: Draft +
-  * First Published at: https://wiki.php.net/rfc/short-syntax-for-closures +
-  * Other formats ..+
  
-===== Introduction ===== +You've followed a link to a topic that doesn't exist yet. If permissions allow, you may create it by using the Create this page button.
- +
-In some cases it is necessary that you have to pass an anonymus function to a function/method as a parameter. If your library receives uses an anonymus functionand alternatively nested anonymus functions, having the function keyword makes the code very long and unecessary unreadable. If you take a look at other languages (Ruby, Python, Groovy, Coffescript) there is a short syntax for closure using the language state block container. Maybe PHP should adopt this behaviour to make code more readable and maintainable. +
- +
-===== Syntax ===== +
- +
-An anonymus function in php could be expressed by a typical statement block, surrounded by curly brackets. +
- +
-<code php> +
-<?php +
-$sayHi = { echo "hi"; }; +
-$sayHi(); // prints: hi +
- +
-$sayHello = { $name => echo "hello, $name"; }; +
-$sayHello("Chuck Norris"); // prints: hello, Chuck Norris +
- +
-$sayHello = { $name, $mood => echo "hello, $name. It's $mood day!"; }; +
-$sayHello("Chuck Norris", "wonderful"); // prints: hello, Chuck Norris. It's a wonderful day! +
-</code> +
- +
-===== Passing an anonymus function to a function ===== +
- +
-The syntax allows for a cleaner way to pass a an anonymus function around. +
- +
-<code php> +
-<?php +
- +
-setFormattingStrategy({$string => +
-    return trim(strtolower($string)); +
-}); +
- +
-</code> +
- +
-===== What is common use cases in other language ===== +
-==== Building domain specific languages ==== +
- +
-Some languages like Groovy and Ruby allows you to move the anonymus function outside of the list of arguments if it is the last argument of the function call, creating a interesting side effect. +
- +
-<code php> +
-function describe($testedClass, callable $tests) { +
-    print ("$testedClass" . PHP_EOL); +
-    $tests(); +
-+
- +
-function it($testName, callable $test) { +
-    print("    $test" . PHP_EOL); +
-    $test(); +
-+
- +
-// because the last argument is a callable we can now: +
- +
-describe ("BankAccount") { +
-    it ("starts with a zero amount") { +
-        $amount = (new BankAccount())->getAmount(); +
-        if ($amount !== 0) throw new Failure("Expected 0, got $amount"); +
-    } +
-+
-</code> +
- +
-===== Pros and Cons ===== +
- +
-==== Pros ==== +
-  * Good for framework development when more expressive callbacks are needed +
-  * Good for DSLs +
-  * Removes bloating, unnecessary syntax +
-  * Other web languages have similar syntax +
-  * Readable +
- +
-==== Cons ==== +
-  * Yet another alias +
-  * Would take distinctness from block statements +
-  * Not as searchable through search engines +
-  * Patch may be difficult to maintain in future +
- +
-===== Changelog =====+
rfc/short-syntax-for-anonymus-functions.1361277665.txt.gz · Last modified: 2017/09/22 13:28 (external edit)