Closed philip-peterson closed 11 years ago
Hi @philip-peterson , Now you can declare Inner functions
hello = ->
echo("Hello")
there = ->
echo(" there")
$there()
$hello()
Compiled to
$hello = function() {
echo('Hello');
$there = function() {
echo(' there');
};
$there();
};
$hello();
I can see a number of functions issues coming up. The first, which I think is an excellent thing that a language compiling to PHP could implement, is useful inner functions.
In PHP you can do this:
If you then call hello(), it will work, printing "Hello there". If you call hello() a second time, however, you'll get:
Because as much as it looks like one,
there
is not actually an inner function; it's a globally scoped one. It's just not defined until a call to hello() is made, and after that, it persists, which is what causes the "redeclare" error when it tries to run the code again. The result is that PHP doesn't really have inner functions, which defeats the whole point of having syntax for them.Now, if Mammouth is a smart language with a smart compiler, it can do something /useful/ with inner functions for PHP. Something, perhaps, like this:
This would make for enormously nicer code bases. There may be the occasional use case where you need to export a function so normal PHP code can read it without being super weird, but there could be a special syntax for that.