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

Common Lisp does assertions right: you can let `assert' provide a restart which allows the user to fix a problem when it crops up. For example:

  (assert (< index (length some-vector))
    (index)
    "Can't change the element at index ~D, index must be smaller than ~D."
    index
    (length some-vector))
That will print a message when the index is too large and give you the option of providing another index.


Wat's up with calling the variable "index" in the second parameter to assert? What happens when you try to call what's presumably an integer..?


There is no call. assert is a macro, the second parameter is a list of places which the restart can update.


It would be interesting if a language allowed control flow to jump between catches and exceptions with named sort of exceptions.

E.g., imagine in this example that the code code throw an invalid index exception, some calling code could catch that, and supply a new index, and control flow would resume from the throw expression.

This would be a complete mess, but it would be interesting nonetheless :)


That is literally what Common Lisp has and GP describes…

https://en.m.wikibooks.org/wiki/Common_Lisp/Advanced_topics/...


It is always fun when people learn some of the things that common lisp has done for a long time.


I realize you could probably build this in lisp, but this (by default) seems to be missing the part about jumping back to where the exception was raised, instead of resuming flow control from where the restart was defined, iiuc.


There is nothing to build, restarts act on the location where the condition was raised. In a conditions system unwinding is a restart, conditions don't unwind before running handlers.

The ability to update a value would make no sense otherwise.

I repeat, what you describe is literally how common-lisp works. Today. And how it has worked for 40 odd years. Also smalltalk.



Exceptions are a special case of general effect systems which do include the capability you describe.


You can do it easily in any language with coroutines, for example Lua.


You can do it easily in any language with functions and flow control. Restarts and coroutines are just if-errs and CPS-natured code in disguise. The difference is only syntactic, and there are ways to reduce the noise to manageable levels.




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

Search: