Hey all!
I’m trying to construct a large Dictionary<Key, Value> from C++.
Unfortunately Dictionary<Key, Value> uses generic constraints;
// from Swift's source code:
@frozen public struct Dictionary<Key, Value> where Key : Hashable {
...
..which causes the type to not be available in C++ using the Swift/C++ interop.
I created a feature request to support generic constraints here: [cxx-interop] Support generic constraints (e.g. `struct Dictionary<Key, Value> where Key : Hashable`) · Issue #86172 · swiftlang/swift · GitHub
..and I also created a feature request to support swift::Dictionary<K, V> here: [cxx-interop] Support Swift's `Dictionary<K, V>` in C++ (`swift::Dictionary<K, V>`?) · Issue #76134 · swiftlang/swift · GitHub
..since the latter might be "easier" to implement than allowing full generic constraints in C++ (as that implies some protocol support, even if it's just limited to Hashable or Equatable)
Anyways, I wanted to ask if anyone has achieved something like this before?
I tried to use AnyHashable, which is also not exposed to C++, I tried to manually specialize each key type (e.g. struct StringDictionary<Value>, struct IntDictionary<Value>, ...) but this is not really scalable, and still doesn't allow me to access the actual Dictionary value.
For now my best workaround is to box the type (Unmanaged<...>) and create generic helpers in Swift, but this still requires a special Swift handler to unwrap the type.