Hiding types
Steven Schveighoffer via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Fri May 30 13:36:18 PDT 2014
On Fri, 30 May 2014 16:09:59 -0400, Philippe Sigaud
<philippe.sigaud at gmail.com> wrote:
> On Friday, 30 May 2014 at 20:02:40 UTC, Steven Schveighoffer wrote:
>
>> If you want an opaque struct, you need to return it by pointer.
>
> 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.
>
>> Otherwise, the user must be able to know what type it is (otherwise,
>> how would he use it?)
>
> I just fear that by using internal, public, functions, the user might
> get access to a private type. I guess the D answer to that is to make
> foo private also. That makes sense.
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 now realize that I implicitly considered 'private' to be transitive
> (or viral). That is that:
>
> Hidden foo() { return Hidden();}
>
> as foo is returning a value from a private type, it should be considered
> private also. By the compiler, I mean.
No, private just makes the symbol not directly accessible. Since foo is
not private, it's accessible.
-Steve
More information about the Digitalmars-d-learn
mailing list