I am using the swift package manager.
I have a module ModuleA which exports two types: ModuleA and Test.
I have a module ModuleB which defines a single type: Test.
In ModuleB, how can I refer to the type Test from ModuleA?
Ideally, I would like syntax such as #module(ModuleA) to refer directly to the module ModuleA.
Reproducible example:
Package.swift:
// swift-tools-version:5.3
import PackageDescription
let package = Package(
name: "ShadowingTest",
products: [
.library(
name: "ModuleA",
targets: ["ModuleA"]),
.library(
name: "ModuleB",
targets: ["ModuleB"]),
],
dependencies: [
],
targets: [
.target(
name: "ModuleA",
dependencies: []),
.target(
name: "ModuleB",
dependencies: ["ModuleA"]),
]
)
Sources/ModuleA/ModuleA.swift:
public enum ModuleA {}
public struct Test {
public static let module: String = "ModuleA"
}
Sources/ModuleB/ModuleB.swift:
import ModuleA
struct Test {
static let module: String = "ModuleB"
}
func test() {
print(ModuleA.Test.module)
}
Running swift build errors with
Sources/ModuleB/ModuleB.swift:8:19: error: type 'ModuleA' has no member 'Test'
but succeeds when public is removed from the ModuleA enum in ModuleA.
the issue is
ModuleAenum in ModuleA module does not haveTest.When you remove
publicinModuleA enumthenModuleBcannot recognize there is aModuleA enumbecause its access modifier isinternalby default soModuleBrecognizesTeststruct inModuleAinstead of trying to findTestinModuleA enumBonus: There is a answer in SO about access modifiers I think you'll find useful.
EDIT:
If you need to use
ModuleA.Testeven there is an enum namedModuleAthen you can useimport (class|struct|func|protocol|enum) <needed_component>so in your case you should import like this:If you want to use the struct with another name to avoid naming conflicts then you can set a typealias ->