This is the code I tried to minimize as much as possible:
protocol LengthValidater {
static var minLength: Int { get }
}
protocol Validater: LengthValidater {
associatedtype ValidateType
static func generateValue() -> ValidateType
}
extension Validater {
static func generate() {
print(Self.minLength) // prints correctly
generateValue()
}
}
protocol TextValidateable: Validater where ValidateType == String {}
extension TextValidateable {
static func generateValue() -> String {
print(Self.minLength)
return ""
}
}
class TextValidater: TextValidateable {
class var minLength: Int {
fatalError()
}
}
class UserIdentifier: TextValidater {
override class var minLength: Int {
return 10
}
}
Calling UserIdentifier.generate()
will crash the app, while it clearly should not since it should use dynamic dispatching and call the overridden class var
. When I remove the associated type and/or return type of generateValue()
, it does not crash.
Why does it crash?