# How to define opaque typealias to link implementation-only type (C-enum use case)

**URL:** <https://forums.swift.org/t/how-to-define-opaque-typealias-to-link-implementation-only-type-c-enum-use-case/69369>\
**Category:** Using Swift\
**Tags:** swift-evolution, c, library-evolution\
**Created:** [January 10, 2024, 2:15pm UTC](https://forums.swift.org/t/how-to-define-opaque-typealias-to-link-implementation-only-type-c-enum-use-case/69369 "2024-01-10T14:15:16Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![oliviermartin](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/oliviermartin/32/32485_2.png) [@oliviermartin](https://forums.swift.org/u/oliviermartin)\
**Post date:** [January 10, 2024, 2:15pm UTC](https://forums.swift.org/t/how-to-define-opaque-typealias-to-link-implementation-only-type-c-enum-use-case/69369/1 "2024-01-10T14:15:16Z")

</div>

Context: I am trying to create a distributable swift library that depends on a C-library.

In my C library, I have enums and structures:

```swift
typedef enum { ONE, TWO } my_numbers_t;
typedef struct {
  int left;
  int right;
} my_pair_t;

```

I created a Swift system library `MyCLibrary` that package my C library and imported it into my Swift target `MySwiftSDK` as `@_implementationOnly import MyCLibrary`.

I understand these C-types cannot be exposed in my public API of my swift module `MySwiftSDK`. Otherwise, I would get errors such as `cannot use struct 'my_pair_t' here; 'MyCLibrary' has been imported as implementation-only`

Using `typealias` does not work:

```swift
typealias MyNumbers = my_numbers_t
typealias MyPair = my_pair_t

```

... the compiler triggers the same errors (fair enough)

Question: How to do it? How to be able to have something like that:

```swift
@_implementationOnly import MyCLibrary`

open class MySuperSDK {
  public func myMethod(number: MyNumbers, pair: MyPair) {
  }
}

```

---

<div class="post-metadata">

**Author:** ![technogen](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/technogen/32/45489_2.png) [@technogen](https://forums.swift.org/u/technogen)\
**Post date:** [January 10, 2024, 3:00pm UTC](https://forums.swift.org/t/how-to-define-opaque-typealias-to-link-implementation-only-type-c-enum-use-case/69369/2 "2024-01-10T15:00:29Z")

</div>

Basically, you have to decide between "is exposed to client code" and "is hidden from client code" and stick to that decision all the way. In the `MySuperSDK` class, you have decided to hide the `MyCLibrary` from the client code by importing it as `@_implementationOnly`, but then you've gone back on your decision by using types from that library in the signature of `myMethod(number:pair:)` method.

You either have to:

1. Expose the C library in order to use those types directly (maybe splitting it into a bunch of smaller libraries to limit the API surface that will be exposed).
2. Make independent wrappers for every declaration you're importing from the C library.

For instance:

```swift
@_implementationOnly import MyCLibrary

public struct MyNumbers: Sendable, Hashable {

    // MARK: MyNumbers - UnderlyingValue

    internal typealias UnderlyingValue = MyCLibrary.my_numbers_t

    internal init(underlyingValue: UnderlyingValue) { 
        self. underlyingValue = underlyingValue
    }

    internal let underlyingValue: UnderlyingValue
}

extension MyNumbers: {

    // MARK: MyNumbers - Known

    public static let one = Self(underlyingValue: MyCLibrary.ONE)

    public static let two = Self(underlyingValue: MyCLibrary.TWO)
}

```

---

<div class="post-metadata">

**Author:** ![allevato](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/allevato/32/385_2.png) [@allevato](https://forums.swift.org/u/allevato)\
**Post date:** [January 10, 2024, 3:16pm UTC](https://forums.swift.org/t/how-to-define-opaque-typealias-to-link-implementation-only-type-c-enum-use-case/69369/3 "2024-01-10T15:16:40Z")

</div>

If I remember correctly, even using an `@_implementationOnly import`ed type as an `internal/private` stored property of a `public struct` will fail, because the compiler needs to load `MyCLibrary` for downstream dependents to know the _layout_ of the property so that it can determine the layout of the `struct`. In other words, ABI is also a factor, not just public API. `@_implementationOnly` doesn't abstract this out today.

This only holds for _structs_ (and enums); if `MyNumbers` were a _class_, the ABI isn't affected because the storage is always indirect. But making it a class isn't a good solution here since you presumably want the usual value semantics.

The only options you have available in this case, to my knowledge, are to wrap the stored property in an `Any` (and thus incur some minor performance penalty when boxing/unboxing the value), or if the C type is small enough, just reproduce it in Swift and write initializers to convert between them.

---

<div class="post-metadata">

**Author:** ![tera](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tera/32/38201_2.png) [@tera](https://forums.swift.org/u/tera)\
**Post date:** [January 10, 2024, 4:01pm UTC](https://forums.swift.org/t/how-to-define-opaque-typealias-to-link-implementation-only-type-c-enum-use-case/69369/4 "2024-01-10T16:01:44Z")

</div>

> [@oliviermartin](#):
>
> I understand these C-types cannot be exposed in my public API of my swift module `MySwiftSDK`.

IIUC there are two possibilities to achieve that (if desired):

1. NS\_ENUM (this if for enum only, not for a struct):

```swift
    typedef NS_ENUM(int, my_numbers_t) { one, two };

```

1. C++ fying your enum and struct along with enabling C++ interop:

```swift
    enum class my_numbers_t { one, two };

    struct my_pair_t {
        int left;
        int right;
    };

```

---

<div class="post-metadata">

**Author:** ![technogen](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/technogen/32/45489_2.png) [@technogen](https://forums.swift.org/u/technogen)\
**Post date:** [January 10, 2024, 7:11pm UTC](https://forums.swift.org/t/how-to-define-opaque-typealias-to-link-implementation-only-type-c-enum-use-case/69369/5 "2024-01-10T19:11:50Z")

</div>

> [@allevato](#):
>
> If I remember correctly, even using an `@_implementationOnly import`ed type as an `internal/private` stored property of a `public struct` will fail

Unless there's some undocumented behavior, according to [the documentation for `@_implementationOnly`](https://github.com/apple/swift/blob/main/docs/ReferenceGuides/UnderscoredAttributes.md#_implementationonly), it should merely introduce compile-time checks to prevent any declaration from the imported module from being part of the API or the ABI.

As long as the `struct` isn't `@frozen` and the stored property is not `@usableFromInline` (if I'm not missing anything), an internal stored property should not be part of the ABI, due to accessor thunks being generated by the compiler (at least in library evolution mode).

> [@allevato](#):
>
> the compiler needs to load `MyCLibrary` for downstream dependents to know the _layout_ of the property so that it can determine the layout of the `struct`.

Yes, but unless the `struct` is `@frozen` (if I'm not missing anything), that layout is not part of the ABI (at least in library evolution mode), no?  
I thought the whole deal with Swift `struct`s (especially compared to C structs) is that they **don't** expose their layout, they wrap all properties into computed property thunks and merely expose the runtime allocation size of the struct (e.g. for use by dynamic-sized stack allocation) specifically for the purpose of allowing structs to be ABI-resilient against non-API changes.

Isn't that the case?

---

<div class="post-metadata">

**Author:** ![allevato](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/allevato/32/385_2.png) [@allevato](https://forums.swift.org/u/allevato)\
**Post date:** [January 10, 2024, 7:35pm UTC](https://forums.swift.org/t/how-to-define-opaque-typealias-to-link-implementation-only-type-c-enum-use-case/69369/6 "2024-01-10T19:35:11Z")

</div>

Good point—the "at least in library evolution mode" is the key bit there. If you're building with library evolution disabled (the default behavior), then non-public stored properties are still part of the layout/ABI; they're generated more or less the same as if they were `@frozen`.

I don't do much work in library evolution mode, so I haven't tested to see if `@_implementationOnly import` with `-enable-library-evolution` allows the use you described. (`@_implementationOnly import` is still useful without library evolution, to trim the size of the build graph, which is the use case I'm most familiar with.)

Given that `@_implementationOnly` is an unsupported and not-fully-implemented feature, I wouldn't necessarily _rely_ on it always working as these characteristics might lead us to expect, though.

---

<div class="post-metadata">

**Author:** ![oliviermartin](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/oliviermartin/32/32485_2.png) [@oliviermartin](https://forums.swift.org/u/oliviermartin)\
**Post date:** [January 10, 2024, 9:38pm UTC](https://forums.swift.org/t/how-to-define-opaque-typealias-to-link-implementation-only-type-c-enum-use-case/69369/7 "2024-01-10T21:38:47Z")

</div>

> [@technogen](#):
>
> Expose the C library in order to use those types directly (maybe splitting it into a bunch of smaller libraries to limit the API surface that will be exposed).

@technogen I am curious about this statement "expose the C library". From my experimentation, I did not manage to expose my static library. I encapsulate my C static library into a SPM `systemLibrary()` but when I import into my `MySwiftSDK` with library evolution (`BUILD_LIBRARY_FOR_DISTRIBUTION=Yes`) the compiler says:

```swift
error: no such module 'MyCLibrary'
import MyCLibrary

```

The only way to fix this issue is to replace `import MyCLibrary` by `@_implementationOnly import MyCLibrary`.  
So I cannot see a way to expose my C library.

My C library looks like that:

In `Package.swift`:

```swift
.systemLibrary(
    name: "MyCLib",
    pkgConfig: "myclib"
),

```

In `Sources/MyCLibrary/module.modulemap`:

```swift
module MyCLibrary [system] {
    header "MyCLibrary-Header.h"
    link "myclibrary"
    export *
}

```

In `Sources/MyCLibrary/MyCLibrary-Header.h`:

```swift
#include "myclibrary.h"

```

---

<div class="post-metadata">

**Author:** ![technogen](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/technogen/32/45489_2.png) [@technogen](https://forums.swift.org/u/technogen)\
**Post date:** [January 10, 2024, 9:54pm UTC](https://forums.swift.org/t/how-to-define-opaque-typealias-to-link-implementation-only-type-c-enum-use-case/69369/8 "2024-01-10T21:54:53Z")

</div>

> [@oliviermartin](#):
>
> @technogen I am curious about this statement "expose the C library". From my experimentation, I did not manage to expose my static library.

SwiftPM supports [Objective-]C[++] targets. Each target has a [`publicHeadersPath`](https://developer.apple.com/documentation/packagedescription/target/publicheaderspath) (that defaults to `include`). As long as you don't mix Swift code with [Objective-]C[++] code in a single target and make sure to put all public headers and the [module map](https://clang.llvm.org/docs/Modules.html#module-maps) in the public headers folder, SwiftPM will build the [Objective-]C[++] code in that target into a proper module, which you can import from Swift code by adding it as a dependency, just like any other dependency.

> [@oliviermartin](#):
>
> I encapsulate my C static library into a SPM `systemLibrary()`

As the documentation says:

> Use system library targets to adapt a library installed on the system to work with Swift packages. Such libraries are generally installed by system package managers (such as Homebrew and apt-get) and exposed to Swift packages by providing a `modulemap` file along with other metadata such as the library’s `pkgConfig` name.

So, unless the library comes with something like a [homebrew formula](https://formulae.brew.sh), what you really want is an [Objective-]C[++] target in SwiftPM, instead of a system library.

---

<div class="post-metadata">

**Author:** ![oliviermartin](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/oliviermartin/32/32485_2.png) [@oliviermartin](https://forums.swift.org/u/oliviermartin)\
**Post date:** [January 10, 2024, 10:53pm UTC](https://forums.swift.org/t/how-to-define-opaque-typealias-to-link-implementation-only-type-c-enum-use-case/69369/9 "2024-01-10T22:53:29Z")

</div>

Thanks @technogen , I am learning a lot of new things with my thread. As far as I can see SPM `target` only knows how to handle [Objective-]C[++] **source** files - but not archive.

I tried to drop my static library into the folder and it did not seem to handle it:

- My `Package.swift`:

```swift
        .target(
            name: "MyCLibrary"
        ),

```

- My `Sources/MyCLibrary` directory contains:

- `Sources/MyCLibrary/include/MyCLibrary-Header.h`
- `Sources/MyCLibrary/libmyclibrary.a`
- `Sources/MyCLibrary/module.modulemap`

... with `Sources/MyCLibrary/module.modulemap`

```swift
module MyStaticLib {
    header "MyStaticLib-Header.h"
    link "mystaticlib"
    export *
}

```

But reading a bit more about SPM `target` with [Objective-]C[++], it does not really works to incorporate large existing out-of-tree C SDK - example [`headerSearchPath`](https://developer.apple.com/documentation/packagedescription/csetting/headersearchpath(_:_:)) expect a relative include directory.  
So I guess my initial approach of abusing of `systemLibrary` and `pkg-config` to expose my out-of-tree C SDK is currenly the best approach.

And after reading all the great contributions from my initial thread, there is no straightforward conversion from C struct/enum type to an opaque swift type.

---

<div class="post-metadata">

**Author:** ![technogen](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/technogen/32/45489_2.png) [@technogen](https://forums.swift.org/u/technogen)\
**Post date:** [January 10, 2024, 10:58pm UTC](https://forums.swift.org/t/how-to-define-opaque-typealias-to-link-implementation-only-type-c-enum-use-case/69369/10 "2024-01-10T22:58:12Z")

</div>

> [@oliviermartin](#):
>
> So I guess my initial approach of abusing of `systemLibrary` and `pkg-config` to expose my out-of-tree C SDK is currenly the best approach.

If you're okay with only targeting Apple platforms, the best way of using pre-built libraries with SwiftPM is to wrap them into XCFramework bundles and use them via a binary target.

You can learn how to do that by running the `xcodebuild -create-xcframework -help` command.
