Specialization incorrectly uses the default implementation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
protocol Factory { | |
associatedtype Value = Self | |
static func make() -> Value | |
} | |
extension Factory { | |
static func make() -> Value { | |
fatalError("In default static implementation") | |
} | |
} | |
struct EmptyFactory: Factory { | |
static func make() -> EmptyFactory { | |
return self.init() | |
} | |
} | |
struct GenericFactory<T, V>: Factory { | |
let variableT: T? = nil | |
let variableV: V? = nil | |
static func make() -> GenericFactory<T, V> { | |
return self.init() | |
} | |
} | |
extension Dictionary: Factory { | |
static func make() -> Dictionary<Key, Value> { | |
return self.init() | |
} | |
} | |
func testSpecialization<F: Factory>(type: F.Type) { | |
let v = F.make() | |
print(v) | |
} | |
print(Dictionary<String, String>.make()) // Uses dictionary implementation | |
testSpecialization(type: EmptyFactory.self) // Uses EmptyFactory Implementation | |
testSpecialization(type: GenericFactory<String, String>.self) // Uses GenericFactory<String> Implementation | |
// This invocation is the bug. It will use the protocol's default implementation | |
testSpecialization(type: Dictionary<String, String>.self) // Uses default implementation | |
// Rename Factory.Value to Factory.Value2 and the default implementation is correctly found. | |
// And curiously enough, this does not have any issue. | |
struct GenericFactoryWithValueConflict<T, Value>: Factory { | |
let variableT: T? = nil | |
let variableV: Value? = nil | |
static func make() -> GenericFactoryWithValueConflict<T, Value> { | |
return self.init() | |
} | |
} | |
testSpecialization(type: GenericFactoryWithValueConflict<String, String>.self) // Uses GenericFactoryWithValueConflict<String, String> Implementation | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment