effekt-lang / effekt

A language with lexical effect handlers and lightweight effect polymorphism
https://effekt-lang.org
MIT License
335 stars 24 forks source link

Allow `with def` analogously to `with val` #526

Closed marzipankaiser closed 4 months ago

marzipankaiser commented 4 months ago

This adds with def in analogy to with val and allows us to write the Cell example (https://effekt-lang.org/examples/named.html) like this:

interface Cell { 
  def get(): Int
  def set(n: Int): Unit 
}

def add {c1: Cell} {c2: Cell} {dst: Cell} = 
  dst.set(c1.get + c2.get)

def fresh[T](init: Int) { prog: {Cell} => T }: T = {
  var contents: Int = init;
  try { prog {c} } with c: Cell { 
    def get() = resume(contents)
    def set(n) = { contents = n; resume(()) }
  }
}

def main() = {
  with def target = fresh(0)
  with def c1 = fresh(0)
  with def c2 = fresh(0)
  c1.set(1);
  c2.set(2);
  add {c1} {c2} {target};
  println(target.get)
}

TODOs after merging this