Crosscut

Daily Thought - 2024-07-12

Hey, I'm Hanno! These are my daily thoughts on Crosscut, the programming language I'm creating. If you have any questions, comments, or feedback, please get in touch!

This thought was published before Crosscut was called Crosscut! If it refers to "Caterpillar", that is the old name, just so you know.

< back to list

Okay, so destructors are not a full alternative to linear types, but what about something like Go's defer statement? That allows you to schedule a function to be executed at the end of the current scope.

Again, this is a solution that works in many cases, but it's not a full replacement for linear types. First off, you could forget to do it. Yes, it's easier to remember, if you can put your deferred cleanup right next to where you created the thing. But that's not a guarantee. Second, it only works for resources you want to clean up in the current scope.

With linear types, the compiler forces you to use a value exactly once. And returning it from the function is using it once. Then the caller can deal with it, or return it again, or put it into a list to deal with later. Anything really. You have full control about where you finally handle it, and yet can't forget about it.

<< previous thoughtnext thought >>

Hey, you! Want to subscribe to my daily thoughts? Just let me know (maybe include a nice message, if you're up for it), and I'll send you an email whenever I post a new one.