Last active
November 19, 2019 19:54
-
-
Save jayrhynas/9f0fcdcf8d7e94bb9154abfc7b7039dd to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// This simple case works fine | |
protocol BasicValueProvider { | |
associatedtype Value | |
static var value: Value { get } | |
} | |
struct False_Basic: BasicValueProvider { | |
static var value: Bool { false } | |
} | |
/*****************/ | |
// The inherited case cannot infer the type of `Value`: | |
protocol ThrowingValueProvider { | |
associatedtype Value | |
static func getValue() throws -> Value | |
} | |
protocol ValueProvider: ThrowingValueProvider { | |
static var value: Value { get } | |
} | |
extension ValueProvider { | |
static func getValue() throws -> Value { | |
return self.value | |
} | |
} | |
struct False: ValueProvider { | |
typealias Value = Bool // !!!ERROR!!! if this is not specified, the compiler emits the errors below | |
static var value: Bool { false } | |
} | |
/* | |
ValueProvider.swift:32:8: error: type 'False' does not conform to protocol 'ValueProvider' | |
struct False: ValueProvider { | |
^ | |
ValueProvider.swift:34:16: note: candidate has non-matching type 'Bool' | |
static var value: Bool { false } | |
^ | |
ValueProvider.swift:32:8: error: type 'False' does not conform to protocol 'ThrowingValueProvider' | |
struct False: ValueProvider { | |
^ | |
ValueProvider.swift:23:16: note: protocol requires property 'value' with type 'False.Value'; do you want to add a stub? | |
static var value: Value { get } | |
^ | |
ValueProvider.swift:18:20: note: protocol requires nested type 'Value'; do you want to add it? | |
associatedtype Value | |
^ | |
*/ |
This file contains hidden or 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
// The solution is to add another associatedtype to the sub-protocol | |
protocol ThrowingValueProvider { | |
associatedtype Value | |
static func getValue() throws -> Value | |
} | |
protocol ValueProvider: ThrowingValueProvider { | |
associatedtype _Value | |
static var value: _Value { get } | |
} | |
extension ValueProvider { | |
static func getValue() throws -> _Value { | |
return self.value | |
} | |
} | |
struct False: ValueProvider { | |
static var value: Bool { false } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment