Scriptor / pharen

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

Missing "use" keyword for Plambdas #49

Closed girvo closed 12 years ago

girvo commented 12 years ago

Hey all.

Loving the plambda functionality but I'd like to be able to "use" variables from an outside scope. See below for how it is used currently. It is very handy in PHP 5.3 and the Slim framework relies on it for certain things...

$app->get("/", function () use ($app) {
    $app->doSomething();
}

What I propose is a syntax like the following:

(plambda [app] ()
    (print-r app))

Where there is a list of variables either before or after the brackets. Or something similar.

I'd love to try and hack this in myself, but I'm struggling to work out how (currently looking at PlambdaDefNode)

Scriptor commented 12 years ago

Hey girvo, sorry for the delay in responding.

plambda shouldn't be necessary anymore, as Pharen's regular lambdas now support interop with the rest of PHP. It should work as long as you have PHP 5.3 or greater. For the first code sample you should be able to just do:

(-> app (get "/"
             (lambda ()
                     (-> app (doSomething)))))

app will be brought in since lambda supports lexical scope. In the guts of Pharen, lambdas return instances of the class PharenLambda, which can be called like functions thanks to the __invoke() magic method.

girvo commented 12 years ago

Oh brilliant. I came up with a bit if a hack to get around it using the Lexical object, so this is really nice :) Sent via BlackBerry® from Telstra

-----Original Message----- From: Tamreen Khan notifications@github.com Date: Wed, 31 Oct 2012 10:20:17 To: Scriptor/pharenpharen@noreply.github.com Reply-To: Scriptor/pharen reply@reply.github.com Cc: girvojosh.girvin@gmail.com Subject: Re: [pharen] Missing "use" keyword for Plambdas (#49)

Hey girvo, sorry for the delay in responding.

plambda shouldn't be necessary anymore, as Pharen's regular lambdas now support interop with the rest of PHP. It should work as long as you have PHP 5.3 or greater. For the first code sample you should be able to just do:

(-> app (get "/"
             (lambda ()
                     (-> app (doSomething)))))

app will be brought in since lambda supports lexical scope. In the guts of Pharen, lambdas return instances of the class PharenLambda, which can be called like functions thanks to the __invoke() magic method.


Reply to this email directly or view it on GitHub: https://github.com/Scriptor/pharen/issues/49#issuecomment-9953624