Hiding types
Philippe Sigaud via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sat May 31 00:58:31 PDT 2014
>> What do you mean? Like this?
>>
>> Hidden* foo() { return new Hidden();}
>
> Yes, this way you can control all aspects of the construction and use. You
> wouldn't need to make it private even, just don't lay out the struct in the
> normal import:
>
> struct Hidden;
>
> I think you would need to use a .di file to do this.
OK, I'll try that also. Thank you!
> You can make the struct's methods and data all private, which would prevent
> any useful access to it. But I don't know your use case.
I was trying to imitate some Haskell code in D. The idea was to play
with resources: from a File, create an OpenedFile which cannot be
created except by calling File.open(). And only FileOpened has a
.close() method.
Or something like this, anyway.
But I now realize that I was heading in a wrong direction: private is
not the way to do that: I want the user to know OpenedFile exists. I
just forbid him to create such a value except through File.open(). So
I guess :
struct OpenedFile {
@disable this...
}
is the way to go.
Now I just have to find an explanation on how to use this. I never
touched this part of D.
More information about the Digitalmars-d-learn
mailing list