rfc:short-syntax-for-anonymus-functions
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
rfc:short-syntax-for-anonymus-functions [2013/02/19 12:41] – created md | rfc:short-syntax-for-anonymus-functions [2017/09/22 13:28] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== | + | ===== This topic does not exist yet ===== |
- | * Version: 1.0 | + | |
- | * Date: 2008-03-06 | + | |
- | * Author: Marcello Duarte < | + | |
- | * Status: Draft | + | |
- | * First Published at: https:// | + | |
- | * Other formats .. | + | |
- | ===== Introduction ===== | + | You've followed a link to a topic that doesn' |
- | + | ||
- | In some cases it is necessary that you have to pass an anonymus function | + | |
- | + | ||
- | ===== Syntax ===== | + | |
- | + | ||
- | An anonymus function in php could be expressed by a typical statement block, surrounded by curly brackets. | + | |
- | + | ||
- | <code php> | + | |
- | <?php | + | |
- | $sayHi = { echo " | + | |
- | $sayHi(); // prints: hi | + | |
- | + | ||
- | $sayHello = { $name => echo " | + | |
- | $sayHello(" | + | |
- | + | ||
- | $sayHello = { $name, $mood => echo " | + | |
- | $sayHello(" | + | |
- | </ | + | |
- | + | ||
- | ===== 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)); | + | |
- | }); | + | |
- | + | ||
- | </ | + | |
- | + | ||
- | ===== 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, | + | |
- | print (" | + | |
- | $tests(); | + | |
- | } | + | |
- | + | ||
- | function it($testName, | + | |
- | print(" | + | |
- | $test(); | + | |
- | } | + | |
- | + | ||
- | // because the last argument is a callable we can now: | + | |
- | + | ||
- | describe (" | + | |
- | it (" | + | |
- | $amount = (new BankAccount())-> | + | |
- | if ($amount !== 0) throw new Failure(" | + | |
- | } | + | |
- | } | + | |
- | </ | + | |
- | + | ||
- | ===== 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)