Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I'm guessing you're trying to equate

  try{
    foo()
  }catch(Exception e){
    bar(e)
  }
  
  void foo() {
    throw new exception();
  }
with

  (bar (call/cc foo))

  (defun foo (cont)
    (cont (make-exception))
However, this ignores most details of the problem. In fact, (re-entrant) continuations and exceptions can't be mixed in the same language: Common Lisp has not adopted `call/cc` for one because it's impossible or at least much harder to implement `unwind-protect` (`finally`) in the presence of `call/cc` [0].

The major difference is that continuations allow you to do this:

  (defparam *global*)
  (defparam *shadow-list* (list))
  (defun foo (cont)
    (setf *global* cont))
  (with-open-file (f "~/file.log")
    (loop
      for line = (read-line stream nil 'eof)
      until (eq line 'eof)
      collect (cons line (call/cc foo)))

  (*global* *shadow-list*)
  (*global* *shadow-list*) ;; what is the state of the file here? what if there was an exception reading from the file?
[0] https://groups.google.com/g/comp.lang.lisp/c/1Ggl_BUi3Yg/m/V...


Naw, it's not impossible; you just use dynamic-wind. But to me this has a bit of the spirit of "Now, you have two problems..."


Well, dynamic-wind itself does not do what unwind-protect does. You have to manually "save/resume" the dynamic environment with the after/before clauses of dynamic-wind.

To be fair, it's probably relatively easy to use dynamic-wind to raise some runtime error when trying to resume the continuation after the original context "ended".


It doesn't, no, but yes, that was my thought: write a version of with-open-file implemented in terms of dynamic-wind that raises a runtime error if you try to reenter. Better hope people are using call/cc for exception handling and not threading though...

Pitman's comments on this are in http://www.nhplace.com/kent/PFAQ/unwind-protect-vs-continuat..., Sitaram's response is at http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.79.3..., and Pitman's final response is at http://www.nhplace.com/kent/PFAQ/unwind-protect-vs-continuat....




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: