Scriptor / pharen

Lisp to PHP Compiler
http://pharen.org
BSD 3-Clause "New" or "Revised" License
218 stars 31 forks source link

Passing anonymous function (php 5.3) #37

Closed girvo closed 12 years ago

girvo commented 12 years ago

Hey there, loving Pharen so far, lots of fun!

I'm trying to tie it to the Slim framework, so far so good, but when defining routes I can use php like this:

$app->get("/", function() {
    // Do some stuff here
});

And as far as I can tell, there is no way of doing that in Pharen. If there isn't, that's okay, close this issue, as I can just name the function anyway, but I was curious.

Scriptor commented 12 years ago

Pharen does have closures, however to stay compatible with PHP 5.2 they're implemented as 2-element arrays. Pharen can handle this fine, but for other PHP functions there's no official way on how to manage this problem. There's a possible way to fix this by making a macro, so I'll keep this issue open so that I can come back and write more here later.

Besides a macro, another long-term fix would be to tell the compiler that when PHP 5.3 is used it can use PHP's own anonymous functions or the ability to use objects as callables.Otherwise just naming the functions should work for now.

francescoagati commented 12 years ago

hi Scriptor, i have made a fork of pharen that support form of php 5.3 closures and namespace and foreach. I have also splitter this in external plugin.

You can see at this link: https://github.com/francescoagati/pharen

example of closures: https://github.com/francescoagati/pharen/blob/master/examples/plugin/plambda.phn

example of namespaces: https://github.com/francescoagati/pharen/blob/master/examples/plugin/namespace.phn

example of use: https://github.com/francescoagati/pharen/blob/master/examples/plugin/use.phn

Scriptor commented 12 years ago

Use of plambdas should fix this for now:

(-> app (get "/" (plambda () ))