From fc0d0c096735a468d47fada616a1a25623923afd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lu=C3=ADs=20Portela=20Afonso?= Date: Wed, 11 May 2022 12:57:52 +0100 Subject: [PATCH 1/2] feat(optional): introduce tryMap to throw error when no value --- Sources/YData/Core/Optional.swift | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Sources/YData/Core/Optional.swift b/Sources/YData/Core/Optional.swift index 83d80a0..1b3e1a5 100644 --- a/Sources/YData/Core/Optional.swift +++ b/Sources/YData/Core/Optional.swift @@ -12,4 +12,12 @@ extension Optional { left = right } } + + func tryMap(_ closure: (() -> E)) throws -> Wrapped { + guard let value = wrapped else { + throw closure() + } + + return value + } } From 99388bfb5649a672c7922c2a8aa62d51e18cfc2c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lu=C3=ADs=20Portela=20Afonso?= Date: Wed, 11 May 2022 13:11:56 +0100 Subject: [PATCH 2/2] fix visibility --- Sources/YData/Core/Optional.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/YData/Core/Optional.swift b/Sources/YData/Core/Optional.swift index 1b3e1a5..3f382a4 100644 --- a/Sources/YData/Core/Optional.swift +++ b/Sources/YData/Core/Optional.swift @@ -13,7 +13,7 @@ extension Optional { } } - func tryMap(_ closure: (() -> E)) throws -> Wrapped { + public func tryMap(_ closure: (() -> E)) throws -> Wrapped { guard let value = wrapped else { throw closure() }