Skip to content

Instantly share code, notes, and snippets.

@rydermackay
Created August 18, 2020 21:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rydermackay/148c2441d928e1c0c25813cfae4146d5 to your computer and use it in GitHub Desktop.
Save rydermackay/148c2441d928e1c0c25813cfae4146d5 to your computer and use it in GitHub Desktop.
import Foundation
struct Box<Value> {
let value: Value
}
extension Box {
// 1.
func map<T>(_ f: @escaping (Value) -> T) -> Box<T> {
return Box<T>(value: f(value))
}
// 2.
func map<T>(_ value: T) -> Box<T> {
return map { _ in value }
}
// 3.
func map<T>(_ keyPath: KeyPath<Value, T>) -> Box<T> {
return map { $0[keyPath: keyPath] }
}
}
func test(input: Box<Void>) -> Box<String> {
// Xcode 12b4: output is Box<String> (overload #1)
// Xcode 12b5: output is Box<() -> String> (overload #2, but only when both #2 AND #3 exist!!)
let output = input.map { "foo" }
return output
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment