Path is ambiguous in this context

I use a nice framework called Path.swift. It imports with import Path and declares a Path struct. I went to add some SwiftUI to my code, and so added import SwiftUI. Unfortunately, SwiftUI also declares a Path struct.

var storePath: Path {…}
           //  ^ 'Path' is ambiguous for type lookup in this context	

So I got a "'Path' is ambiguous for type lookup in this context" compile error. I tried to disambiguate it by referring to it as Path.Path, but I get the same error at the first Path.

Then I tried import struct Path.Path. Unfortunately, that doesn’t import some nice extensions, and so other code fails to compile.

Is there a way to say "Use the Path in the module Path?"

1 Like

AFAIK, not yet. See Pitch: Fully qualified name syntax for additional discussion.

3 Likes

Hmm. Seems this would work for me, but alas, there appears to be no way to do this for an Xcode package dependency.

The best workaround today is to add a file to your module that only imports Path-the-module and then declare a typealias for Path-the-type. Then you can use the alias for Path.Path and also refer to SwiftUI.Path.

4 Likes

Alas, this doesn't seem to work. This code won't compile:

	var storePath: SwiftPath {
		return SwiftPath.documents / kAppDatabaseName
               ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~
               error: binary operator '/' cannot be applied to operands of type 'DynamicPath' and 'String'
	}

Path.documents returns a DynamicPath, which is struct DynamicPath : Pathish, just as struct Path : Pathish is. I’m not sure why this fails when Path.documents succeeds. / is a concatenation operator defined like this:

    @inlinable
    static func /<S>(lhs: Self, rhs: S) -> Path where S: StringProtocol {
        return lhs.join(rhs)
    }

That sounds like a bug; a typealias shouldn’t affect operator resolution. (Unless the operator is supposed to be public or something.)

It appears the library you link to already vends public typealias PathStruct = Path.

1 Like

Did you remove or change any import statements in the file with that code?

2 Likes

Oh, yes, silly me. I removed the import Path. I thought the typealias I made would magically bring along everything (e.g. PathStruct.documents would bring in DynamicPath and that would bring in the operator /, all without the need to import the module).

Nice catch. This should solve my problems. And here I went and forked their repo in order to change the name of it in their Package.swift.