effekt
effekt copied to clipboard
Using objects for handler implementations
As mentioned in #365 (here), it should be possible to use objects as handler implementations.
// ...
def handler = new Handler {
def catch[R]{ p: => R / Exc } { h: String => R } =
try { p() } with Exc {
def raise[A](msg) = h(msg)
}
}
def main() = try { p() } with handler;
Currently, this is not possible but should be for tail-resumptive operations.
Related to #252; conceptually try { ... } installs a delimiter and I would prefer having a mechanism to bring handler implicitly into scope (like given instances in Scala).
Discussion in the meeting (@jiribenes came up with this):
def main() = {
with handler;
p()
}
is just sugar for:
def main() = {
handler { p() }
}
so we should be able to call objects with block arguments to mean "handle this".
def main() = {
with x = handler;
p()
}
desugars to
def main() = {
handler { x =>
p()
}
}
def main() = {
with {cap} = handler;
p()
}
should desugar to
def main() = {
handler { {cap} => // still explicit...
p()
}
}