Caterpillar

Daily Thought - 2024-07-12

< 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 >>