Skip to content

dpearson2699/swift-ios-skills / skills/swift-language

swift-language

Verified

Apply modern Swift language patterns and idioms for non-concurrency, non-SwiftUI code. Covers if/switch expressions (Swift 5.9+), typed throws (Swift 6+), result builders, property wrappers, opaque and existential types (some vs any), guard patterns, Never type, Regex builders (Swift 5.7+), basic Codable shaping (CodingKeys, custom decoding, nested containers), modern collection APIs (count(where:), contains(where:), replacing()), basic FormatStyle usage, and string interpolation patterns. Use when writing core Swift code involving generics, protocols, enums, closures, or modern language features; route deep Codable to swift-codable, detailed formatting/localization to swift-formatstyle, and API naming to swift-api-design-guidelines.

FileKindSize
SKILL.mdskill md14.8 KB
evals/evals.jsonconfig4.8 KB
references/swift-attributes-interop.mdmarkdown3.7 KB
references/swift-patterns-extended.mdmarkdown13.2 KB

references/swift-attributes-interop.md

View on GitHub
# Swift Attributes and C Interoperability

Attributes and interoperability features for Swift. Covers C-calling-convention export, module disambiguation, performance annotations, and symbol visibility control.

## Contents

- [C Interoperability — `@c` Attribute](#c-interoperability--c-attribute)
- [Module Selectors](#module-selectors)
- [Performance Annotations](#performance-annotations)
- [Symbol Visibility and Layout](#symbol-visibility-and-layout)

## C Interoperability — `@c` Attribute

For functions, the `@c` attribute (SE-0495) marks a Swift declaration for direct C-calling-convention export. The function becomes callable from C, C++, and Objective-C without bridging headers or `@_cdecl`.

```swift
@c(MyLib_processBuffer)
public func processBuffer(_ buffer: UnsafePointer<UInt8>?, _ count: Int32) -> Int32 {
    guard let buffer else { return 0 }
    return buffer.pointee == 0 ? 0 : count
}
```

**Requirements:**
- Parameters and return types must be C-compatible (primitives, pointers, tuples of C-compatible types)
- No Swift-only types (`String`, `Array`, `UnsafeBufferPointer`, closures, generic placeholders, etc.) in the signature
- The function must be a module-level free function (not a method)
- Use `@c(CustomName)` when the C symbol should differ from the Swift function name
- SE-0495 also supports `@c` enums with C-compatible integer raw types; this section focuses on function export

## Module Selectors

SE-0491 adds `ModuleName::symbolName` syntax to disambiguate identically named symbols from different modules without `import` aliasing.

```swift
import NetworkingA
import NetworkingB

// Both modules export a top-level `configure()` function
func setup() {
    NetworkingA::configure()
    NetworkingB::configure()
}

// Works with types too
let client: NetworkingA::Client = .init()
```

## Performance Annotations

### `@specialized`

SE-0460 makes `@specialized` an official attribute (previously underscored as `@_specialize`). Forces the compiler to emit a specialized version of a generic function for specific concrete types.

```swift
@specialized(where T == Int)
@specialized(where T == Double)
func sum<T: Numeric>(_ values: [T]) -> T {
    values.reduce(.zero, +)
}
```

### `@inline(always)` Guarantee

SE-0496 makes `@inline(always)` a guaranteed inlining request for direct function references. Previously it was a hint the compiler could ignore. A compilation error is now emitted when the compiler can prove direct-call inlining is impossible (e.g., recursive calls).

```swift
@inline(always)
func fastPath(_ x: Int) -> Int {
    x &+ 1  // Guaranteed for direct calls that can be inlined
}
```

The guarantee does not apply to dynamic calls through first-class function
values, protocol values, generic constraints, or non-final class dispatch.

## Symbol Visibility and Layout

### `@export`

SE-0497 gives explicit control over symbol visibility and definition availability:

- `@export(interface)` — ensures a callable symbol exists in the binary but hides the definition from clients (no inlining/specialization by external callers). Replaces `@_neverEmitIntoClient`.
- `@export(implementation)` — makes the definition available for inlining/specialization but does not guarantee a callable symbol. Replaces `@_alwaysEmitIntoClient`.

```swift
@export(interface)
public func stableAPI() -> Int {
    // Callable symbol guaranteed; definition hidden from clients
    return computeValue()
}
```

### `@section` and `@used`

SE-0492 places global or static variables into named binary sections and prevents dead-stripping. Primarily for Embedded Swift and systems programming.

```swift
@section(".mydata") @used
var configFlag: Int32 = 1
```

Use it only on stored variables in non-generic contexts with compile-time-known
or static initialization.
brainstormingobra/superpowersYou MUST use this before any creative work - creating features, building components, adding functionality, modifying behavior, or planning anything new, in software or out of it (a talk, a business, a renovation).Stars: 322kUpdated Claude CodeCodexVerifiedInstall
brand-guidelinesanthropics/skillsApplies Anthropic's official brand colors and typography to any sort of artifact that may benefit from having Anthropic's look-and-feel. Use it when brand colors or style guidelines, visual formatting, or company design standards apply.Stars: 198kUpdated Claude CodeCodexVerifiedInstall
frontend-designanthropics/skillsGuidance for distinctive, intentional visual design when building new UI or reshaping an existing one. Helps with aesthetic direction, typography, and making choices that don't read as templated defaults.Stars: 198kUpdated Claude CodeCodexVerifiedInstall
mcp-builderanthropics/skillsGuide for creating high-quality MCP (Model Context Protocol) servers that enable LLMs to interact with external services through well-designed tools. Use when building MCP servers to integrate external APIs or services, whether in Python (FastMCP) or Node/TypeScript (MCP SDK).Stars: 198kUpdated Claude CodeCodexVerifiedInstall