Last active
November 15, 2020 19:59
-
-
Save zoecarver/994058601aaf005937b5ad405716550e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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