-
-
Save glaforge/f7ddece9d4e0ff2afe82 to your computer and use it in GitHub Desktop.
@Grab('com.netopyr.coffee4java:coffee4java:1.0.0') | |
import javax.script.ScriptEngineManager | |
def coffeescript = new ScriptEngineManager().getEngineByName('coffeescript').factory.scriptEngine | |
assert coffeescript.eval('''\ | |
square = (x) -> x * x | |
return square 3''') == 9 |
CoffeScript wraps everything in an anonymous function by default to make it harder to pollute the global namespace. I should probably add an option that makes the wrapper optional.
I have this silly idea few day ago that it would be fantastic if you can write some code in CoffeeScript instead of Groovy because as soon as you start to use CoffeeScript more you find a plethora of unnecessary brackets everywhere. I'm totally lame in writing anything non-trivial such as class loaders or or compilers but the idea is
- have a .coffee file in similar structure as the groovy files (but maybe in
src/main/coffee
instead ofsrc/main/groovy
) - when requested (hooking to the class loader?) parse the .coffee file to CoffeeScript AST and from it generate either a .groovy file or directly the Groovy script on the fly using byte code manipulation
The point is to be able to call Groovy code from CoffeeScript and on the other hand to be able to declare classes in CoffeeScript which can be called later from Groovy.
And idea how to achieve with small effort e.g. global AST executed in very early phase?
It's strange I have to use return to actually return a value, as square 3 wouldn't.
But that's perhaps my CoffeeScript which is weak!