logoalt Hacker News

Closures in Tcl

78 pointsby andsoitisyesterday at 1:08 PM26 commentsview on HN

Comments

theszyesterday at 5:31 PM

  > Well, I've encountered this use case a few times in Lisp:...
  > ...where a callback is used to collect various items.
This can be and is achieved by simple SQL-like query. Filter (flat) set of nodes by integerness and you even do not need a push_back.

Despite that, I find article interesting. It shows that Tcl can truely be multiparadigm programming language.

Myself, I've implemented pattern matching [1] over algebraic-type-like values and used that here and there.

[1] https://wiki.tcl-lang.org/page/Algebraic+Types

show 1 reply
tialaramexyesterday at 11:46 PM

> In C++, this could be achieved if all local variables were in fact std::shared_ptr captured by value.

So, in C++ you do actually get to pick what happens and there are plenty of options but for our purposes here all we want is a (mutable) reference capture.

However, experienced C++ programmers would never do this because C++ is all foot guns all the time, so you can express what you meant and it'll blow up and cause chaos because now our reference outlives the thing referred to. Oops.

In Rust we can write what we meant, but instead of the program exploding at runtime the compiler will politely point out that this can't work and why.

And so armed with the knowledge from that, we can (in Rust or with C++ although it's harder to spell in C++) write something that'll actually work.

We could move the captured variable. In Rust we just use the keyword `move`, now the captured variable is gone, moved inside the closure, and so as with the Tcl the same variable (the one moved into this closure) is used each time the closure is called, and if we make another closure that's got a different captured variable.

But we could do the "shared reference" trick, that type is spelled Rc in Rust.

RHSeegeryesterday at 11:40 PM

> You might wonder why you'd ever need such a strange behaviour, right?

Closures can also be used to return a group of methods that all act on the same set of variables; ie, objects.

gitroomyesterday at 6:05 PM

Pretty cool seeing folks show up about Tcl, tbh I messed with it ages ago and never thought people were still this into it

show 1 reply
dingnutsyesterday at 3:12 PM

Is Tcl having a revival? Anybody know where Tclers hang out online?

show 8 replies