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

Added support for "use" keyword in closures #69

Merged
merged 9 commits into from
Apr 24, 2019

Conversation

sergeyklay
Copy link
Contributor

@sergeyklay sergeyklay commented Apr 24, 2019

Example of supported forms:

An empty closure with "use" keyword
namespace Example;
class Closure
{
    public function callback()
    {
        var abc = 42;
        return function () use (abc) { };
    }
}
A closure with "use" keyword and body
namespace Example;
class Closure
{
    public function callback()
    {
        var abc = 42;
        return function () use (&abc) {
            var r;
            let r = abc;
            let abc = null;
            return r;
        };
    }
}
A closure with params, "use" keyword and empty body
namespace Example;
class Closure
{
    public function callback()
    {
        var abc = 42;
        return function (one, two, three) use (abc) { };
    }
}
A closure with params, with "use" keyword and non empty body
namespace Example;
class Closure
{
    public function callback()
    {
        var abc = 42;
        return function (one, two, three) use (const & abc) {
            return [
                one,
                two,
                three,
                abc
            ];
        };
    }
}
A complex example with "use" keyword
class Closure
{
    public function callback(const resource &fp, const string filter, array &debug)
    {
        var line, cb;
        let line = fgets(fp);

        let cb = function (array matches) use ( & debug, const filter ) {
            let debug += [matches];
            if (matches[0] !== filter) {
                return strtolower(matches[0]);
            }

            return matches[0];
        };

        return preg_replace_callback("|<p>\s*\w|", cb, line);
    }
}

Refs:

/cc @phalcon/zephir-team

@codecov
Copy link

codecov bot commented Apr 24, 2019

Codecov Report

Merging #69 into development will increase coverage by 3.31%.
The diff coverage is 82%.

Impacted file tree graph

@@               Coverage Diff               @@
##           development      #69      +/-   ##
===============================================
+ Coverage        32.34%   35.66%   +3.31%     
===============================================
  Files                5        5              
  Lines             4399     4436      +37     
===============================================
+ Hits              1423     1582     +159     
+ Misses            2976     2854     -122
Impacted Files Coverage Δ
parser/parser.h 44.25% <100%> (+2.72%) ⬆️
parser/zephir.lemon 16.13% <76.31%> (+4.05%) ⬆️
parser/scanner.re 50.09% <0%> (+2.91%) ⬆️
parser/scanner.c 39.51% <0%> (+3.44%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 5eb10f1...5b6bada. Read the comment docs.

@sergeyklay sergeyklay merged commit 0339ec2 into development Apr 24, 2019
@sergeyklay sergeyklay deleted the feature/closure-use branch April 24, 2019 19:31
@Jurigag
Copy link

Jurigag commented Apr 24, 2019

As far as i know we currently don't support references, does it mean that in related zephir issue we gonna implement it as well?

@sergeyklay
Copy link
Contributor Author

@Jurigag It is a possibility for my future plans.

n.b. The language scanner and parser have supported this syntactic structure since the very first days. So I don't introduced a brand new grammar

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

Successfully merging this pull request may close these issues.

2 participants