Skip to content

Instantly share code, notes, and snippets.

@zoecarver
Last active November 15, 2020 19:59
Show Gist options
  • Save zoecarver/994058601aaf005937b5ad405716550e to your computer and use it in GitHub Desktop.
Save zoecarver/994058601aaf005937b5ad405716550e to your computer and use it in GitHub Desktop.
// C++
template<class T>
struct MyVector {
using element_type = T;
using iterator_type = T*; // or some trait.
element_type operator[](unsigned i) { ... }
iterator_type begin() { ... }
iterator_type end() { ... }
};
// Imported as...
struct MyVector<T> {
typealias element_type = T
typealias iterator_type = // Either a pointer or through some importer magic we get an iterator.
func begin() -> iterator_type
// You get the idea...
}
// Used as (before type checking)...
extension MyVector : VectorManualModel { ... }
func convertToSwiftArray<Vect: VectorManualModel>(_ vect: Vect) -> [Vect.ElementType] {
var out = [Vect.ElementType]()
var iter = vect.begin()
let endIter = vect.end()
while iter != endIter { out.append(iter.dereference()) iter = iter.next() }
return out
}
func caller<Unrelated>(...) {
let v: MyVector<Int> = ...
convertToSwiftArray(v)
}
// After specialization...
struct __CxxSpecailzied_MyVector { ... }
func __CxxSpecialized_convertToSwiftArray(_ vec: MyVector<Int>) -> [Int] { ... }
func caller<Unrelated>(...) {
let v: MyVector<Int> = ...
__CxxSpecialized_convertToSwiftArray(v)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment