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 improvements #151

Merged
merged 4 commits into from
Mar 29, 2024
Merged

Closure improvements #151

merged 4 commits into from
Mar 29, 2024

Conversation

brachy84
Copy link
Member

  • Adds a mixin to Closure so which delegates the call method to GroovySandbox so we never have to worry about forgetting that.
  • Adds helper methods in ClosureHelper to add a variable holder an to get the under lying closure from a functional interface object.
  • cleans up some other code

Example:
Add this to Crafting (oor anywhere else)

public void test(BooleanSupplier supplier) {
        Closure<?> closure = ClosureHelper.getUnderlyingClosure(supplier);
        ClosureHelper.withEnvironment(closure, new TestEnv(), true);
        GroovyLog.get().info("Supplier: {}", supplier.getAsBoolean());
}

Add TestEnv class

private static class TestEnv {

        public final String testVar = "environment variable";

        public void testFunc() {
            GroovyLog.get().info("environment method");
        }
}

In a script add

crafting.test {

    log.info(testVar)
    testFunc()

    return true
}

@brachy84 brachy84 merged commit 2e7753f into master Mar 29, 2024
1 check passed
@brachy84 brachy84 deleted the closure-magic branch March 29, 2024 10:47
@brachy84 brachy84 added enhancement New feature or request sandbox For internal changes to the sandbox labels Apr 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request sandbox For internal changes to the sandbox
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant