Implementing typestate

Idan Arye via Digitalmars-d digitalmars-d at puremagic.com
Wed Sep 16 08:34:38 PDT 2015


On Wednesday, 16 September 2015 at 14:34:05 UTC, Ola Fosheim 
Grøstad wrote:
> On Wednesday, 16 September 2015 at 10:31:58 UTC, Idan Arye 
> wrote:
>> What's wrong with two `open()`s in a row? Each will return a 
>> new file handle.
>
> Yes, but if you do it by mistake then you don't get the 
> compiler to check that you call close() on both. I should have 
> written "what if you forget close()". Will the compiler then 
> complain at compile time?
>
> You can't make that happen with just move semantics, you need 
> linear typing so that every resource created are consumed 
> exactly once.

Move semantics should be enough. We can declare the destructor 
private, and then any code outside the module that implicitly 
calls the d'tor when the variable goes out of scope will raise a 
compilation error. In order to "get rid" of the variable, you'll 
have to pass ownership to the `close` function, so your code 
won't try to implicitly call the d'tor.


More information about the Digitalmars-d mailing list