[swift-evolution] Opaque Pointers in Swift

Cory Benfield cbenfield at apple.com
Wed Oct 25 02:33:15 CDT 2017



> On 24 Oct 2017, at 18:23, Johannes Weiß <johannesweiss at apple.com> wrote:
> 
> How do people think about this proposed change?

I think keeping type information on OpaquePointer would be extremely useful, and definitely improves some of the sharp edges of that type.

> this should solve your problem:
> 
> - when you receive a pointer from the C library, store it as OpaquePointer
>    let myOpaquePointer: OpaquePointer = OpaquePointer(c_library_create_function())
>  which now should work regardless of whether you're linking the old or the new version of the C library
> - when you pass a pointer to the C library:
>    c_library_consuming_function(.init(myOpaquePointer))
>  which should (modulo it doesn't right now https://bugs.swift.org/browse/SR-6211) select the right initialiser for you, only it doesn’t 😲

Yeah, this approach is probably acceptable. I’m going to stop short of calling it “good”, because frankly it remains a pretty unfortunate hack. It would be nicer if there was a way to tell the Swift compiler that the user should never initialise the structure, regardless of whether it’s opaque (that is, to tell the Swift compiler that when it sees SSL_CTX * it should translate that to OpaquePointer<SSL_CTX> instead of UnsafePointer<SSL_CTX>), but I will concede that adding support for that case is likely to be a bit too niche for the community to have much interest.

In the absence of that better version, your proposed interface will get the job done without being too gross. Thanks!

Cory



More information about the swift-evolution mailing list