[swift-users] Importing Empty C Structs

Jeff Kelley slaunchaman at gmail.com
Fri Feb 12 09:52:29 CST 2016


OK, that makes sense. I printed the dynamic type of git_repository_open() and the first parameter translates from git_repository* to UnsafeMutablePointer<COpaquePointer>. So I can make a typealias from git_repository to COpaquePointer. I’m wondering if I should submit a proposal to swift-evolution to automate that process.


Jeff Kelley

SlaunchaMan at gmail.com <mailto:SlaunchaMan at gmail.com> | @SlaunchaMan <https://twitter.com/SlaunchaMan> | jeffkelley.org <http://jeffkelley.org/>
> On Feb 12, 2016, at 4:42 AM, Jeremy Pereira <jeremy.j.pereira at googlemail.com <mailto:jeremy.j.pereira at googlemail.com>> wrote:
> 
> 
>> On 12 Feb 2016, at 06:41, Jeff Kelley via swift-users <swift-users at swift.org <mailto: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 <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
> 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20160212/af57b851/attachment.html>


More information about the swift-users mailing list