Created
August 23, 2018 01:28
-
-
Save sberrevoets/a9185d71cd22b5748ac7af9d8fd42553 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
final class Storage<StoredValue> { | |
private var storage: [StoredValue] = [] | |
func addFailingValue<T: ModelRepresentable>(_ model: T) where T.Model == StoredValue { | |
self.storage.append(model.value) | |
} | |
func addValue<T: ModelRepresentable>(_ model: T) { | |
guard let value = model.value as? StoredValue else { return } | |
self.storage.append(value) | |
} | |
} | |
protocol ModelRepresentable { | |
associatedtype Model | |
var value: Model { get } | |
} | |
struct SimpleModel: ModelRepresentable { | |
let value = "test" | |
} | |
let storage = Storage<SimpleModel>() | |
storage.addValue(SimpleModel()) // works as expected | |
storage.addFailingValue(SimpleModel()) // generic parameter 'T' could not be inferred |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment