How to Disambiguate a Type and a Module With the Same Name

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

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<Int>() // from Foundation
let bar = BTreeOrderedSet<Int>() // from BTree

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

How to disambiguate

As @Alexander has highlighted, you can use the module name that defines B. Since B is likely to be defined in your project, you can use the project's name to access the top-level class:

class A {
}

class B {
enum BEnum {}
}

extension A {
enum B {
static let x = ProjectName.B.BEnum.self
}
}

Alternatively, if you are defining a package, replace ProjectName with your ModuleName.

How does python disambiguate between module paths and member functions

With import mod you are in practice binding the results of that search to a name in the local scope

>>> import numpy 
>>> numpy
<module 'numpy' from 'etc/site-packages/numpy/__init__.py'>

With mod = [3,2,1] you reassign mod to a list, so now mod is a list

>>> numpy = [1,2]
>>> numpy
[1, 2]

Thus, with mod.sort() you are calling the sort method on a list.

For additional details on the import system you can refer to the documentation.

How to disambiguate between traits with the same name?

Rather than use T::Balance here, you need to be more specific for the Rust compiler.

You can do <T as balances::Trait>::Balance or <T as assets::Trait>::Balance to specify which "Balance" you actually want to use.

Let me know if this helps!

Import two exported classes with the same name

You can use as like this:

import {Class1} from '../location1/class1'
import {Class1 as Alias} from '../location2/class1'

You can find more about the ES6 import statement here.

Does F# naming conventions forbids having the same name inside different disc. union types?

When you open thingB module, type OneB comes into scope and the Common case label from type OneA gets shadowed with the one from type OneB.

When name clashes between types or union/active pattern cases occur, the most recent one wins. Reordering the opens would make it work by chance:

open thingB
open thingA

The right solution is to prefix the case name. There's also RequireQualifiedAccess attribute you can use to force a type (or module) to always require prefixes for its internals.



Related Topics



Leave a reply



Submit