Incomplete types question

via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Jul 8 04:16:51 PDT 2014


On Tuesday, 8 July 2014 at 10:47:26 UTC, NoUseForAName wrote:
> Also side question. What are these .di files? D propaganda 
> radio told me that D does not need header files because it has 
> a real module system (like e.g. Pascal or Go) yet in the 
> solution Ali posted these .di files seem to be.. pretty much 
> like C header files.

They are optional, for those situations when you don't want to 
distribute the entire source code (e.g. proprietary library 
distributed in binary form). They are usually auto-generated by 
the compiler (dmd -H), and I agree that it's awkward to have to 
define them manually.

> So I could just put..
> 
> public struct S;
> 
> private struct S
> {
>     int i;
> }
> 
> .. in the .d file and that would work? I mean I expected 
> something like this but then why would Ali post such a complex 
> solution?

I guess because it's a (the only?) way to get real incomplete 
types. It is the most "literal" translation of what you 
described. But of course, there are better ways to achieve what 
you actually want.

Your example above doesn't work, because it would be interpreted 
as a redefinition of the struct. Instead, you can use a public 
alias to a private type:

// my_module.d:

private struct MyStructImpl {
     int x;
}

public alias MyStructPtr = MyStructImpl*;

void doSomething(MyStructPtr foo) {
     ...
}

// my_main_program.d:

MyStructPtr bar;    // OK
MyStructImpl foo;   // Error: my_module.MyStructImpl is private


More information about the Digitalmars-d-learn mailing list