[Grace-core] Assignment return values and null

James Noble kjx at ecs.vuw.ac.nz
Tue Jun 26 06:01:20 PDT 2012


> method loop(body) {
>    def myBreakException = new Exception
>    try {
>        def break = { -> throw myBreakException }
>        while {true} do {
>            body.apply(break)
>        }
>    } catch { (myBreakException) -> }
> }

you can do this with catch/throw (which I don't think is implemented yet)
or you can do it just with blocks - but unfortunately the "loop" code gets
even denser than your version.

http://gracelang.org/applications/2012/05/02/loop-with-exits-revisited/

but, interestingly, the code using the loop looks just the same 

> loop { break ->
>    def line = myfile.getline; if (myfile.eof) { break.apply() }
>    print(line)
> }

I do think we should consider the indefinite "loop", at least, for the standard library.
You can use it with return, provided you factor just the loop into a method.

method readAndPrintLines {
  loop {
     def line = myfile.getLine
     if (myfile.eof) then {return}
     print(line)
  }
}


> def fac10 = recurse { me, n -> if (n == 1) then { 1 } else { n * me.apply(n - 1) } } with (10)

yeah, that's cute. I think Clojure has a similar recur macro to get tail-recursion on the JVM.

> I'll shut up now...


no, it's cool to have you around & see what people are getting up to!

James 



More information about the Grace-core mailing list