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

前端 未结 2 1929
-上瘾入骨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 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 = BTree.OrderedSet
    

     

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

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

提交回复
热议问题