Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closure issue [$25] #888

Closed
ghost opened this issue Apr 14, 2015 · 11 comments
Closed

Closure issue [$25] #888

ghost opened this issue Apr 14, 2015 · 11 comments
Labels
nfr New Feature Request

Comments

@ghost
Copy link

ghost commented Apr 14, 2015

Zephir\ParseException: Syntax error in *******.zep on line 10

  let result = array_filter(array_keys(all_list), function(item) use (only_list) {
-------------------------------------------------------------------^
@Green-Cat
Copy link
Contributor

Zephir does not support use() for closures.
As a work around you can put the parameters in an array and use call_user_func_array.

@KonstantinKuklin
Copy link

@Green-Cat Do you plan to support generators and coroutines?

@valVk
Copy link

valVk commented May 4, 2015

how could we convert that syntax to the correct one?

For example I have this

class Functional {

    public static function array_map_recursive(callback, value) 
    {
        if (is_array(value)) {
            return array_map(function(value) use (callback) { return array_map_recursive(callback, value); }, value);
        }
        return call_user_func(callback, value);
    }

    public static function test_rec (item) 
    {
        return item . " +1";
    }
}

Thanks.

@baszczewski
Copy link
Contributor

Use keyword is extremely useful. It allow us to inherit variables from the parent scope. Is there any plans to support it?

@fezfez
Copy link
Contributor

fezfez commented Jul 12, 2015

Any news ?

@phalcon phalcon changed the title Closure issue Closure issue [$10] Aug 29, 2015
@phalcon phalcon added the bounty label Aug 29, 2015
@phalcon phalcon changed the title Closure issue [$10] Closure issue [$25] Sep 6, 2015
@kristoftorfs
Copy link

Is there any news on this issue yet? I'm currently doing a rather large project in Phalcon, but would like to use Zephir instead of PHP for development. However, this is kind of a major issue (for me personally).

@dkeepersun
Copy link

yeah ,its really useful . so many closure i use in my project. its will be very important feature!

@kse300489
Copy link

kse300489 commented May 5, 2016

Right now it can't be added to zephir, becouse anonimus functions in zephir it is not Closure like in php, it is simple function name. When you write in zephir:

return array_map(function(var item){ return item + 1; }, value);

Zephir simple create new function with unredable name, like:

function func_101193d7181cc88340ae5b2b17bba8a1(var item) {
  return item + 1;
}

and then use it like this

return array_map('func_101193d7181cc88340ae5b2b17bba8a1', value);

So anonimus functions in zephir it simple named functions.

@Jurigag
Copy link
Contributor

Jurigag commented Apr 7, 2017

Could this be somehow implemented @sjinks ? Lately i added factories to phalcon so for example we can do something like:

use Phalcon\Db\Adapter\Pdo\Factory;

$config = new Phalcon\Config\Ini('config.ini');
$di->setShared('db', Factory::load($config->database));

But obviously on this point it will create database adapter object, i just wonder if this could be implemented so i could add some method like loadAsCallback which will simply be:

public static function loadAsClosure(options) 
{
    return function() use (options) {
        return Factory::load(options);
    }
}

Or there is some other workaround for this? Is something like this gonna work:

public static function loadAsClosure(options) 
{
    var f;
    
    let this->options = options;

    let f = function() {
        return Factory::load(this->options);
    }

    f->bindTo(this);

    return f;
}

?

Well already can tell it won't work.

@sergeyklay sergeyklay added duplicate and removed bounty nfr New Feature Request labels Apr 7, 2017
@sergeyklay sergeyklay reopened this Apr 7, 2017
@sergeyklay sergeyklay added nfr New Feature Request and removed duplicate labels Apr 7, 2017
@kran
Copy link

kran commented Jan 29, 2019

use bindTo to solve this:

var dummy;
let dummy = new \stdClass();
let dummy->foo = "some variable";

(function(){
    var dummy = <\stdClass> this;
    echo dummy->foo;
})->bindTo(dummy);

@sergeyklay
Copy link
Contributor

Should work in current development branch. Thank you for the contributing, and for helping us make Zephir better. Feel free to open a new in case of any issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
nfr New Feature Request
Projects
None yet
Development

No branches or pull requests