I first noticed this in a Cocoa App Xcode 10.1 (10B61) project. I (essentially) got a compiler error saying
Bar
is not a member of typeFoo
although I had a file Foo.swift
containing:
enum Foo { }
extension Foo {
enum Bar { }
}
and a file Bar.swift
containing:
extension Foo.Bar {
static func baz() { print("baz") }
}
The problem turned out to be that Foo.swift
happened to be listed after Bar.swift
in Target -> Build Phases -> Compile Sources, and the solution was to drag Bar.swift
before Foo.swift
in the list of Compile Sources.
Question: Should the project compile or not depending on the order of files in the Compile Sources list?
Or (afaics) equivalently: Program a.swift compiles, but b.swift does not compile, is that intended behavior?
a.swift:
extension Foo {
enum Bar { }
}
extension Foo.Bar {
static func baz() { print("baz") }
}
enum Foo { }
Foo.Bar.baz()
b.swift: (same as a.swift except the two extensions have swapped places)
extension Foo.Bar {
static func baz() { print("baz") }
}
extension Foo {
enum Bar { }
}
enum Foo { }
Foo.Bar.baz()
$ swiftc --version
Apple Swift version 4.2.1 (swiftlang-1000.11.42 clang-1000.11.45.1)
Target: x86_64-apple-darwin18.2.0
$ swiftc a.swift && ./a
baz
$ swiftc b.swift && ./b
b.swift:1:15: error: 'Bar' is not a member type of 'Foo'
extension Foo.Bar {
~~~ ^
b.swift:8:1: error: type 'Foo.Bar' has no member 'baz'
Foo.Bar.baz()
^~~~~~~ ~~~