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
103 lines
1.3 KiB
Swift
103 lines
1.3 KiB
Swift
@frozen
|
|
public struct PublicThing<T> {
|
|
@_specialize(exported: true, where T == Double)
|
|
@inlinable
|
|
public func doStuffWith(_ t: T) {
|
|
print(t)
|
|
}
|
|
public init(_ t: T) {}
|
|
}
|
|
|
|
@frozen
|
|
public struct PublicPlainThing {
|
|
public init() {}
|
|
}
|
|
|
|
@usableFromInline
|
|
@frozen
|
|
internal struct BoxedThing<T> {}
|
|
|
|
@usableFromInline
|
|
@frozen
|
|
internal struct BoxedThing2<T> {
|
|
var t: T
|
|
|
|
init(_ t: T) {
|
|
self.t = t
|
|
}
|
|
}
|
|
|
|
|
|
@usableFromInline
|
|
@frozen
|
|
internal struct InternalThing<T> {
|
|
@inlinable
|
|
func doStuffWith(_ t: T) {
|
|
print(t)
|
|
}
|
|
|
|
@_specialize(exported: true, where T == Double)
|
|
@inlinable
|
|
func doStuffWith(boxed: BoxedThing<T>) {
|
|
print(boxed)
|
|
}
|
|
|
|
@inlinable
|
|
func doStuffWith(boxed2: BoxedThing2<T>) {
|
|
print(boxed2)
|
|
}
|
|
|
|
@usableFromInline
|
|
init(_ t: T) {}
|
|
}
|
|
|
|
public struct ResilientThing {
|
|
public init() {}
|
|
}
|
|
|
|
@usableFromInline
|
|
@frozen
|
|
internal struct InternalThing2<T> {
|
|
@usableFromInline
|
|
var x : T
|
|
|
|
init(_ t: T) {
|
|
x = t
|
|
}
|
|
|
|
@inlinable
|
|
var computedX : T {
|
|
return x
|
|
}
|
|
|
|
@inlinable
|
|
var computedY : T {
|
|
get {
|
|
return x
|
|
}
|
|
set {
|
|
x = newValue
|
|
}
|
|
}
|
|
|
|
@inlinable
|
|
var computedZ : T {
|
|
_modify {
|
|
yield &x
|
|
}
|
|
_read {
|
|
yield x
|
|
}
|
|
}
|
|
|
|
@inlinable
|
|
subscript(_ i: Int) -> T {
|
|
get {
|
|
return x
|
|
}
|
|
set {
|
|
x = newValue
|
|
}
|
|
}
|
|
}
|