Last active
October 22, 2020 10:38
-
-
Save yutailang0119/eab69e00c9b9f7ccefd6d139cc7be3e3 to your computer and use it in GitHub Desktop.
AをBに変換するCombine.Publisher拡張を作りたい
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
import UIKit | |
import Combine | |
struct A { | |
let name: String | |
} | |
struct B { | |
let text: String | |
} | |
protocol Convertable { | |
associatedtype V | |
var converted: V { get } | |
// or | |
// associatedtype V | |
// func converted() -> V | |
// or | |
// func converted<V>() -> V | |
} | |
extension Publisher { | |
func converted() -> Publishers.Map<Self, Self.Output.V> where Self.Output == Convertable { | |
self.map { $0.converted } | |
} | |
} | |
extension A: Convertable { | |
var converted: B { | |
.init(text: self.name) | |
} | |
// or | |
// func converted() -> B { | |
// .init(text: self.name) | |
// } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment