Lets say I have a class:
Foo {
always()
onlyScopeB()
}
And I have different methods, which take different closures: scopeA, scopeB
foo = new Foo()
scopeA{
foo.always() // this should COMPILE
foo.onlyScopeB() // this should NOT COMPILE
}
scopeB{
foo.always() // this should COMPILE
foo.onlyScopeB() // this should COMPILE
}
Is there anyway to achieve this at the compilation stage? I am writing a DSL and I have scopes that correspond to stages in a process and sometimes fields are null in one scope, and then other times they are not-null and I am trying to provide the best semantic experience to find errors easily.

If you want to achieve this at the compilation stage, the only way I can think of is to write a custom AST Transformation.
Edit: The best source for learning about AST transformations is too look at the ones from Groovy itself: https://github.com/apache/groovy/tree/master/src/main/java/org/codehaus/groovy/transform