Andy and I did a bit of brainstorming tonight on nailing down the scoping rules for closure blocks. Part of this discussion is a feature I'm calling "with blocks". The goal of with blocks is similar to Groovy style builders or C# constructor initialization blocks - to write declarative code within the Fan language and make it easy to build declarative style DSLs. Examples include build manifests, configuration files, serialization, XML, HTML, GUI code, etc. The fundamental principle of a with block is just like a "with" statement in Visual Basic - it changes the scope of "this":
So any expression can be followed a {} block, which uses the result of the expression to change the scope of this. Using with blocks with factory methods and constructors makes it easy to create and initialize nested object structures.
One of the tricky things we need to figure out is how "this" scoping works in both with blocks and in closures. Today I haven't really nailed down the scoping rules. We need to decide if "this" is always the inner scope or the outer scope. And to come up with a syntax for the alternate. Consider:
x.times |Int i| { echo("$this $type"); }
Today this and type refer to the closure instance and type, not the outer method's scope. Changing virtual methods like type or toStr to scope from the outside first can be confusing, although is probably less astonishing. And either way we still need a syntax to scope the non-default - either maybe a keyword or something like Java like Outer.this.
brian Mon 26 Jun 2006
Andy and I did a bit of brainstorming tonight on nailing down the scoping rules for closure blocks. Part of this discussion is a feature I'm calling "with blocks". The goal of with blocks is similar to Groovy style builders or C# constructor initialization blocks - to write declarative code within the Fan language and make it easy to build declarative style DSLs. Examples include build manifests, configuration files, serialization, XML, HTML, GUI code, etc. The fundamental principle of a with block is just like a "with" statement in Visual Basic - it changes the scope of "this":
So any expression can be followed a {} block, which uses the result of the expression to change the scope of this. Using with blocks with factory methods and constructors makes it easy to create and initialize nested object structures.
One of the tricky things we need to figure out is how "this" scoping works in both with blocks and in closures. Today I haven't really nailed down the scoping rules. We need to decide if "this" is always the inner scope or the outer scope. And to come up with a syntax for the alternate. Consider:
Today this and type refer to the closure instance and type, not the outer method's scope. Changing virtual methods like type or toStr to scope from the outside first can be confusing, although is probably less astonishing. And either way we still need a syntax to scope the non-default - either maybe a keyword or something like Java like Outer.this.