Last active
April 16, 2020 11:40
-
-
Save MrSkwiggs/82eb119f235f63396cca40314ef423be to your computer and use it in GitHub Desktop.
?? Operator for multiple-optionals - Overloads that extends the default ?? operator behaviour up to triple optionals
This file contains hidden or 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
// | |
// Created by Dorian Grolaux on 14/01/2020. | |
// Copyright © 2020 Skwiggs Inc. All rights reserved. | |
// | |
import Foundation | |
/** | |
Nil coalesces 2-level optionals in a single step | |
*/ | |
func ??<T> (optional: T??, defaultValue: @autoclosure () throws -> T) rethrows -> T { | |
return try optional as? T ?? defaultValue() | |
} | |
/** | |
Nil coalesces 2-level optionals in a single step | |
*/ | |
func ??<T> (optional: T??, defaultValue: @autoclosure () throws -> T?) rethrows -> T? { | |
return try optional as? T ?? defaultValue() | |
} | |
/** | |
Nil coalesces 3-level optionals in a single step | |
*/ | |
func ??<T> (optional: T???, defaultValue: @autoclosure () throws -> T) rethrows -> T { | |
return try optional as? T ?? defaultValue() | |
} | |
/** | |
Nil coalesces 3-level optionals in a single step | |
*/ | |
func ??<T> (optional: T???, defaultValue: @autoclosure () throws -> T?) rethrows -> T? { | |
return try optional as? T ?? defaultValue() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Examples
Double optional
Now
Before
Triple optionals
Now
Before