2 ms·
This seems similar to replicating substructural type systems like linear or affine types via interfaces. I can't remember who gave this talk, but a prime examp
by tonyhb 3y ago
This seems similar to replicating substructural type systems like linear or affine types via interfaces.
I can't remember who gave this talk, but a prime example is a `File` type. A `File` type shouldn't have a Read() method until the file itself has opened. And calling `Close()` on the File should prevent further reads. Substructural typing - and this pattern - allows you to refine types based off of the type's instance state.
There's a ton possible here but I haven't really kept up to date with type systems in the last 5 years. Stuff like this has probably advanced a ton!
- atilaneves 3y agoWouldn't the instance state here be "was constructed"? That is: you should always be able to call `Read` because the constructor opened the file, and you shouldn't have to call `Close` anyway because the destructor did.
- tonyhb 3y agoI suppose it depends on the API. Some languages require grabbing a handle and calling open. Either way, theory is the same!