33 lines
746 B
Swift
33 lines
746 B
Swift
//
|
|
// Optional+Extension.swift
|
|
//
|
|
//
|
|
// Created by Juraldinio on 08.08.2022.
|
|
//
|
|
|
|
import Foundation
|
|
|
|
public extension Optional {
|
|
|
|
var isExist: Bool {
|
|
if case .some = self {
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
|
|
func orCreate(_ creation: @autoclosure () -> Wrapped) -> Wrapped {
|
|
switch self {
|
|
case let .some(value): return value
|
|
case .none: return creation()
|
|
}
|
|
}
|
|
|
|
func orTypedCreate<Element: RawRepresentable>(_ creation: @autoclosure () -> Element) -> Element where Element.RawValue == Wrapped {
|
|
switch self {
|
|
case let .some(value): return Element(rawValue: value) ?? creation()
|
|
case .none: return creation()
|
|
}
|
|
}
|
|
}
|