Download the PHP package md/lambda-preprocessor without Composer
On this page you can find all versions of the php package md/lambda-preprocessor. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package lambda-preprocessor
A hack-like lambda syntax for PHP
In Hacklang, a lambda expression is denoted by using the lambda arrow ==>
. Left of the arrow are arguments to the anonymous function and on the right hand side is either an expression or a list of statements.
This pre-processor is meant to make that syntax available in native PHP applications. This does not happen at runtime, so there is no hit on perfomance. The pre-processor works on top of the pre plugin and the yay macro library.
Syntax wise, and due to the fact that Yay does not have an expresison parser yet, even though Hack supports a single expression to be written without the need of curly brakets, this pre-processor will always require the curly brakets to be written. Hopefully this will change once Yay develops further.
Oneliner anonymous function
The simplest example is an anonymous function that returns the argument it was given in the first place. In PHP it could be written as:
Which can be written, using this pre-processor, much simpler:
Should we need to declare typehints or pass more than one argument, we will need to use parenthesis. We could also use parenthesis in the example above, but it is optional for single arguments with no type hinting.
Which is translated to:
Or with type hints:
Which is translated to:
Multiple lines anonymous functions
The same rules for arguments, typehints and return types still apply for anonymous functions with multiple lines. The only difference is that you will need to write the return
keyword in the last statement of the block, and you must now end every statement with a semicolon.
This is translated into:
Closures
PHP provides the keyword use
to capture variables from the enclosing scope. Hack's lambdas make this even easier. You can use variables implicitly inside of a lambda. This is available when you use this pre-processor.
Note that to use $lastname
with the official syntax you would have to use the use
keyword, as shown below:
Recursive calls and precedence
The pre-processor will translate the inner block and then the outer. The precedence with this syntax is a bit more obvious than the hack one. In hack you can ommit the brakets {
, }
. Here, it should be straightforward that the expression:
will be translated into:
Final Notes
- This pre-processor is alpha software. Do not use it in production.