Implementing typestate

Idan Arye via Digitalmars-d digitalmars-d at puremagic.com
Wed Sep 16 09:24:48 PDT 2015


On Wednesday, 16 September 2015 at 15:57:14 UTC, Ola Fosheim 
Grøstad wrote:
> On Wednesday, 16 September 2015 at 15:34:40 UTC, Idan Arye 
> wrote:
>> 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.
>
> Sounds plausible, but does this work in C++ and D? I assume you 
> mean that you "reinterpret_cast" to a different type in the 
> close() function, which is cheating, but ok :).

No need for `reinterpret_cast`. The `close` function is declared 
in the same module as the `File` struct, so it has access to it's 
private d'tor.


More information about the Digitalmars-d mailing list