Avatar
extension Optional { public mutating func consume<R>(_ f: (Wrapped) throws -> R) rethrows -> R? { guard let x = self else { return nil } self = nil return try f(x) }
👀 1
4:02 AM
↑最近こんなの作って使ってみてるんだけど使い所がかなり多くてめちゃ便利
4:03 AM
self.cameraCapture.consume { $0.dispose() } // ↓と同じ if let c = self.cameraCapture { c.dispose() self.cameraCapture = nil } (edited)
4:04 AM
guard let capture = (self.cameraCapture.consume { $0 }) else { return } use(capture) // ↓と同じ guard let capture = self.cameraCapture else { return } self.cameraCapture = nil use(capture)