Created
November 9, 2019 20:14
-
-
Save rustle/9182ec6f65b5035b8467c4958b10d4ad to your computer and use it in GitHub Desktop.
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 Example { | |
associatedtype Value | |
func value() -> Value | |
} | |
struct AnyExample<Value> { | |
private class Container<Value> { | |
func value() -> Value { | |
fatalError() | |
} | |
} | |
private final class _Container<ExampleType: Example>: Container<ExampleType.Value> { | |
override func value() -> ExampleType.Value { | |
example.value() | |
} | |
private let example: ExampleType | |
init(example: ExampleType) { | |
self.example = example | |
} | |
} | |
private let upstream: Container<Value> | |
init<ExampleType: Example>(upstream: ExampleType) where ExampleType.Value == Value { | |
self.upstream = _Container(example: upstream) | |
} | |
func value() -> Value { | |
upstream.value() | |
} | |
} | |
extension Example { | |
func eraseToAnyExample() -> AnyExample<Value> { | |
AnyExample(upstream: self) | |
} | |
} | |
struct AnExample: Example { | |
typealias Value = String | |
func value() -> String { | |
"hello" | |
} | |
} | |
struct ExampleMember { | |
// Want to do this: | |
//let example1: Example<String> | |
// or | |
//let example2: Example where Value == String | |
// maybe an opaque return type plus where clause? | |
//let example3: some Example where Value == String | |
// Instead have to do this | |
let example4 = AnyExample(upstream: AnExample()) | |
// or | |
let example5 = AnExample().eraseToAnyExample() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment