4 ms·
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 (< in
by tmtvl 2y ago
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.
- mort96 2y agoWat'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..?
- masklinn 2y agoThere is no call. assert is a macro, the second parameter is a list of places which the restart can update.
- foota 2y agoIt 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 :)
- skavi 2y agoExceptions are a special case of general effect systems which do include the capability you describe.
- masklinn 2y agoThat is literally what Common Lisp has and GP describes… https://en.m.wikibooks.org/wiki/Common_Lisp/Advanced_topics/Condition_System https://en.m.wikibooks.org/wiki/Common_Lisp/Advanced_topics/...
- taeric 2y agoIt is always fun when people learn some of the things that common lisp has done for a long time.
- foota 2y agoI 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.
- masklinn 2y agoThere 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.
- igouy 2y agohttps://cuis-smalltalk.github.io/TheCuisBook/Inspecting-the-Unexpected.html https://cuis-smalltalk.github.io/TheCuisBook/Inspecting-the-... https://cuis-smalltalk.github.io/TheCuisBook/The-Debugger.html https://cuis-smalltalk.github.io/TheCuisBook/The-Debugger.ht... https://cuis-smalltalk.github.io/TheCuisBook/Halt_0021.html https://cuis-smalltalk.github.io/TheCuisBook/Halt_0021.html
- PhilipRoman 2y agoYou can do it easily in any language with coroutines, for example Lua.
- wruza 2y agoYou 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.