effekt icon indicating copy to clipboard operation
effekt copied to clipboard

Using objects for handler implementations

Open dvdvgt opened this issue 1 year ago • 3 comments

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.

dvdvgt avatar Mar 05 '24 10:03 dvdvgt

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".

b-studios avatar Mar 05 '24 16:03 b-studios

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()
  }
}

b-studios avatar Mar 05 '24 16:03 b-studios