koka icon indicating copy to clipboard operation
koka copied to clipboard

Possible bracket mismatch in documentation?

Open Trung0246 opened this issue 3 years ago • 3 comments

It seems like (handler{ fun emit(msg){ println(msg) })( fn(){ hello() } ) in section 3.1.5 don't have correct pairs of bracket which is very confusing.

Another possible bracket mismatch is finally(fn(){ println... }), fn(){ println("entering"); throw("oops") + 42 })

Trung0246 avatar Dec 23 '20 09:12 Trung0246

Ouch, confusing indeed! Thanks for finding this -- I'll fix it :-) Changed to:

  • (handler{ fun emit(msg){ println(msg) } })( fn(){ hello() } )
  • finally(fn(){ println(...) }, fn(){ println("entering"); throw("oops") + 42 })

Any suggestions for documentation improvement is welcome -- always tricky to write in a way that strikes the right balance between explaining from first principles and staying interesting enough :-)

daanx avatar Dec 23 '20 20:12 daanx

If handler desugar like that, then when with keyword desugar to f(e1,...,eN, fn(){ <body> }), wouldn't it be handler(fun () { fun emit(msg){ println(msg) } }, fun(){ hello() });? What's stuff handler will return? The document didn't explain about that much.

Trung0246 avatar Dec 23 '20 21:12 Trung0246

Ah good remark! I had to think twice when I saw your response :-)

handler{ <ops } is a syntactical construct and not a function handler applied to a function block { <ops> } (which makes sense as the <ops> bind the operations and are complex syntactical constructs themselves, much like a match(<expr>){ <cases> } expresssion).

So the expression handler{ <ops> } evaluates to a function with type ( action : () -> <l | e> a ) -> e a for an effect l.

And the desugaring of with handler { <ops> }; action() is thus with (handler{ <ops> }); action() is (handler{<ops>})(fn(){action()})

Hope this helps, I will try to clarify the docs.

(and I agree it is a bit inconsistent: normally braces are just for function blocks but not with match, handler, and within struct /con definitions.)

daanx avatar Dec 24 '20 03:12 daanx