Caterpillar

Daily Thought - 2024-07-14

< back to list

Over the last few days, I've been talking about linear types in general. But the reason I mentioned them initially, was the role they can play in purely functional I/O. Let's see how that can work.

Let's say you call a function that writes to a file, and that file is represented by an opaque value. You need to pass that value to the function. Now you can't use that value ever again, so if you want to read from the file afterwards, that function needs to return an updated value to represent the file after the write. And you pass that to a second function, that reads from it.

As a result, it is now easy to keep track of what's happening and when, both for the developer and the tooling. You see exactly what path the file takes through the functions that operate on it, and in which order those operations happen. If, for example, the runtime decided to run some of that code in parallel, it knows exactly where it can do that, and what needs to stay serial.

<< previous thoughtnext thought >>