I have following code that compiles in Swift 4.2, but doesn't in Swift 5.0:
public enum OneOf2<T1, T2> {
case t1(T1), t2(T2)
}
// MARK: - Protocols
public protocol _OneOf2Type {
associatedtype T1; associatedtype T2
static func value(_ v: T1) -> Self
static func value(_ v: T2) -> Self
}
// MARK: - _OneOf_Type
extension OneOf2: _OneOf2Type {
public static func value(_ v: T1) -> OneOf2<T1, T2> { return .t1(v) }
public static func value(_ v: T2) -> OneOf2<T1, T2> { return .t2(v) }
}
It seems that somehow generic argument T1
in OneOf2
doesn't tie-up to _OneOf2Type.T1
.
The weird thing is that these are the following compilation results (in order that I tried them):
- On Xcode 10.1 using Xcode 10.1 toolchain: code compiles nicely;
- On Xcode 10.2 using Xcode 10.2 toolchain: code never finishes compiling (and spent few hours trying to minimize, compile and recompile the code);
- On Xcode 10.2 using Xcode 10.1 toolchain: code doesn't compile and I get compilation errors (which actually led me to a solution below);
This is the change that is required for the code to compile in Xcode 10.2 with Swift 5 (a.k.a. Xcode 10.2 toolchain):
extension OneOf2: _OneOf2Type {
typealias T1 = T1
typealias T2 = T2
public static func value(_ v: T1) -> OneOf2<T1, T2> { return .t1(v) }
public static func value(_ v: T2) -> OneOf2<T1, T2> { return .t2(v) }
}
Just wondering if this is by design and expected change in Swift 5.
Best!
Eimantas