mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
This attribute allows to define a pre-specialized entry point of a
generic function in a library.
The following definition provides a pre-specialized entry point for
`genericFunc(_:)` for the parameter type `Int` that clients of the
library can call.
```
@_specialize(exported: true, where T == Int)
public func genericFunc<T>(_ t: T) { ... }
```
Pre-specializations of internal `@inlinable` functions are allowed.
```
@usableFromInline
internal struct GenericThing<T> {
@_specialize(exported: true, where T == Int)
@inlinable
internal func genericMethod(_ t: T) {
}
}
```
There is syntax to pre-specialize a method from a different module.
```
import ModuleDefiningGenericFunc
@_specialize(exported: true, target: genericFunc(_:), where T == Double)
func prespecialize_genericFunc(_ t: T) { fatalError("dont call") }
```
Specially marked extensions allow for pre-specialization of internal
methods accross module boundries (respecting `@inlinable` and
`@usableFromInline`).
```
import ModuleDefiningGenericThing
public struct Something {}
@_specializeExtension
extension GenericThing {
@_specialize(exported: true, target: genericMethod(_:), where T == Something)
func prespecialize_genericMethod(_ t: T) { fatalError("dont call") }
}
```
rdar://64993425
69 lines
1.7 KiB
Swift
69 lines
1.7 KiB
Swift
import A
|
|
|
|
public class AnotherThing {
|
|
public init() {}
|
|
}
|
|
|
|
@_specializeExtension
|
|
extension InternalThing {
|
|
|
|
@_specialize(exported: true, target: compute() , where T == AnotherThing)
|
|
@_specialize(exported: true, target: compute(), where T == ResilientInternalBoxedThing<AnotherThing>)
|
|
public func specializeCompute() -> T {
|
|
fatalError("don't call")
|
|
}
|
|
|
|
public var specializeComputedX : T {
|
|
@_specialize(exported: true, target: computedX, where T == AnotherThing)
|
|
get {
|
|
fatalError("don't call")
|
|
}
|
|
@_specialize(exported: true, target: computedX, where T == AnotherThing)
|
|
set {
|
|
fatalError("don't call")
|
|
}
|
|
}
|
|
|
|
public subscript(specialized i: Int) -> T {
|
|
@_specialize(exported: true, target: subscript(_:), where T == AnotherThing)
|
|
get {
|
|
fatalError("don't call")
|
|
}
|
|
@_specialize(exported: true, target: subscript(_:), where T == AnotherThing)
|
|
set {
|
|
fatalError("don't call")
|
|
}
|
|
}
|
|
}
|
|
|
|
@_specializeExtension
|
|
extension InternalRef {
|
|
|
|
@_specialize(exported: true, target: compute() , where T == AnotherThing)
|
|
public func specializeCompute() -> T {
|
|
fatalError("don't call")
|
|
}
|
|
|
|
public var specializeComputedX : T {
|
|
@_specialize(exported: true, target: computedX, where T == AnotherThing)
|
|
get {
|
|
fatalError("don't call")
|
|
}
|
|
@_specialize(exported: true, target: computedX, where T == AnotherThing)
|
|
set {
|
|
fatalError("don't call")
|
|
}
|
|
}
|
|
|
|
public subscript(specialized i: Int) -> T {
|
|
@_specialize(exported: true, target: subscript(_:), where T == AnotherThing)
|
|
get {
|
|
fatalError("don't call")
|
|
}
|
|
@_specialize(exported: true, target: subscript(_:), where T == AnotherThing)
|
|
set {
|
|
fatalError("don't call")
|
|
}
|
|
}
|
|
}
|