How can I disambiguate a type and a module with the same name?

前端 未结 2 1928
-上瘾入骨i
-上瘾入骨i 2020-12-04 15:19

I\'m trying to use Károly Lőrentey\'s B-tree based OrderedSet in a project. However, I\'m running into an issue where I can\'t declare an unqualified OrderedSet

相关标签:
2条回答
  • 2020-12-04 15:59

    I renamed OrderedSet to SortedSet in the Swift 3 version of BTree, which should serve as a workaround while a possible language-level fix is being discussed/implemented.

    0 讨论(0)
  • 2020-12-04 16:13

    The type can be disambiguated using the little-known import (class|struct|func|protocol|enum) Module.Symbol syntax.

    import struct BTree.OrderedSet
    

    From this point on, OrderedSet unambiguously refers to the one in BTree.

    If this would still be ambiguous or sub-optimal in some files, you can create a Swift file to rename imports using typealiases:

    // a.swift
    import struct BTree.OrderedSet
    typealias BTreeOrderedSet<T> = BTree.OrderedSet<T>
    

     

    // b.swift
    let foo = OrderedSet<Int>() // from Foundation
    let bar = BTreeOrderedSet<Int>() // from BTree
    

    There was a new syntax discussed for Swift 3, but it fell through.

    0 讨论(0)
提交回复
热议问题