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
wskdf opened this issue Apr 14, 2015 · 11 comments

Comments

Projects
None yet
@wskdf
Copy link

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

This comment has been minimized.

Copy link
Contributor

commented Apr 14, 2015

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

This comment has been minimized.

Copy link

commented Apr 16, 2015

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

@valVk

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link
Contributor

commented May 25, 2015

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

@fezfez

This comment has been minimized.

Copy link
Contributor

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

This comment has been minimized.

Copy link

commented Jan 14, 2016

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

This comment has been minimized.

Copy link

commented May 4, 2016

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

@kse300489

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link
Member

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.

@kran

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link
Member

commented Apr 30, 2019

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.

@sergeyklay sergeyklay closed this Apr 30, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.