-
Notifications
You must be signed in to change notification settings - Fork 10.7k
[SR-79] Allow C function pointer generation in a generic context #42701
Copy link
Copy link
Closed
Labels
Description
| Previous ID | SR-79 |
| Radar | None |
| Original Reporter | @jtbandes |
| Type | Improvement |
| Status | Closed |
| Resolution | Won't Do |
Additional Detail from JIRA
| Votes | 0 |
| Component/s | Compiler |
| Labels | Improvement |
| Assignee | None |
| Priority | Medium |
md5: 2b220b9bcf1aded0a95ce67120821e5d
Issue Description:
It would be really great if the following code worked. It currently doesn't because "a C function pointer cannot be formed from a closure that captures generic parameters".
It seems this should be possible — the compiler could just generate a different copy of the function for each instance of the generic type being used.
import Foundation
class BinaryHeap<T: AnyObject where T: Comparable>
{
let heap: CFBinaryHeap
init() {
var callbacks = CFBinaryHeapCallBacks(
version: 0,
retain: { _, obj in
Unmanaged<T>.fromOpaque(COpaquePointer(obj)).retain()
return obj
},
release: { _, obj in
Unmanaged<T>.fromOpaque(COpaquePointer(obj)).release()
},
copyDescription: { obj in
let o = Unmanaged<T>.fromOpaque(COpaquePointer(obj)).takeUnretainedValue()
return Unmanaged.passRetained(String(o) as CFString)
},
compare: { obj1, obj2, _ in
let o1 = Unmanaged<T>.fromOpaque(COpaquePointer(obj1)).takeUnretainedValue()
let o2 = Unmanaged<T>.fromOpaque(COpaquePointer(obj2)).takeUnretainedValue()
if o1 == o2 { return .CompareEqualTo }
if o1 < o2 { return .CompareLessThan }
return .CompareGreaterThan
}
)
heap = CFBinaryHeapCreate(nil, 0, &callbacks, nil)
}
}Reactions are currently unavailable