3 ms·
My favorite cool feature of Tcl: A return statement that can return by arbitrary number of levels instead of 1. http://www.tcl.tk/man/tcl8.5/TclCmd/return.htm#M
by anonymous246 16y ago
My favorite cool feature of Tcl: A return statement that can return by arbitrary number of levels instead of 1. http://www.tcl.tk/man/tcl8.5/TclCmd/return.htm#M15 http://www.tcl.tk/man/tcl8.5/TclCmd/return.htm#M15
I found this very useful, for example when writing a webapp that expected a session ID in a cookie. When not found, it was supposed to redirect to an error page.
In Tcl:
proc handler {} {
load_session
# do stuff
}
load_session will use two-level return to redirect to the error page.
In python etc:
def handler:
if load_session() == error:
return
# do stuff
Minor, yes, but we're talking language nerdery. :)
- silentbicycle 16y agoContinuations handle this more cleanly. Heck, you can return to an arbitrary point 1 or more functions back on the stack in C, via setjmp/longjmp, when you really need to.
- anonymous246 16y agoCan you explain how continuations apply here. I don't really have a good idea about that. Wrt setjmp/longjmp, I think the key differences are the lack of a 'setjmp' and that you don't have to pass in the setjmp buffer down the calls. If you ignore those aspects and focus only the barest essence, yes they're the same (just like nitrogen and a cake are the same because they're both made of protons, neutrons, and electrons).
- silentbicycle 16y agoThink of a first class continuation as a heap-allocated setjmp buffer - rather than unwinding the stack, it changes the stack to a saved stack (which, since it isn't unwound, can be returned to multiple times). Continuations and longjmp can't return an arbitrary number of levels up the call stack without placing some sort of marker there first, but IMHO that's a good thing. Continuations are a "structured" version of that Tcl return construct. While I haven't done much with Tcl, is it typically used to implement other language constructs with more specific semantics, or is it typically used directly?
- anonymous246 16y agoI've never seen it used (except by myself once). :) Because the control flow becomes hard to understand, and the called function becomes extremely context sensitive. But I think it has its uses within libraries' private functions or in code which can afford to be blub-incompatible.
- m0th87 16y agoThat is so painfully wrong.
- anonymous246 16y agoWould you care to explain?
- m0th87 16y ago1) You have no guarantees that code you write will resume from a method invokation point. 2) How do you do safe resource cleanup? If you open a file and pass the method to a handler, how do you guarantee that you can close it after the method call? 3) This feature seems like it would make compiler optimizations difficult to say the least. But more than anything, it just feels dirty, like goto's.
- anonymous246 16y agoYes, exactly. Which is why I haven't used it ever except in that one case where it turned out to make the code clearer (at least to me).
- abecedarius 16y agoForth's cute that way too -- it gets a little shout-out in _Thinking Forth_.