[swift-users] Importing Empty C Structs
Jeremy Pereira
jeremy.j.pereira at googlemail.com
Fri Feb 12 03:42:03 CST 2016
> On 12 Feb 2016, at 06:41, Jeff Kelley via swift-users <swift-users at swift.org> wrote:
>
>
>> typedef struct git_repository git_repository;
>
> An empty struct. Interesting. A quick sample project in Xcode shows that this line will not generate anything in the Swift generated interface. So it appears to me that this type is invisible to Swift.
This is not an empty struct, it is an example of an incomplete type.
https://en.wikipedia.org/wiki/C_syntax#Incomplete_types
It is a feature of C that you can specify that a struct type exists without giving any information about its internal structure or even size. Typically, a struct definition like the above is placed in a header file and it is redefined in a .c file with all of its internal structure. Compilation units other than the .c file in which the struct is fully defined have know knowledge of what the struct looks like or even how big it is, so all they can do is store pointers to such structs and pass them as parameters and return values.
Any code that manipulates the internals of the struct has to be defined in the implementation file that the struct is fully defined in. Other compilation units can only manipulate the struct through functions that form the API, usually defined in the same header as the incomplete type.
This is a very common pattern in C because it provides complete encapsulation. In fact, it provides better encapsulation than C++ classes.
>
> Jeff Kelley
>
> SlaunchaMan at gmail.com | @SlaunchaMan | jeffkelley.org
>
> _______________________________________________
> swift-users mailing list
> swift-users at swift.org
> https://lists.swift.org/mailman/listinfo/swift-users
More information about the swift-users
mailing list