boudhayan
5/5/2018 - 6:28 PM

Optional.swift

import Darwin
//Declare nil-coalescing operator
infix operator ??

/// Nil-coalescing operator defination
///
/// - Parameters:
///   - optional: optinal variable
///   - defaultValue: additional default value if optional is nil
/// - Returns: If optional has some value, it returns the unwrapped value else returns the default value.

func ??<U>(optional: U?, defaultValue: U) -> U {
    if let value = optional {
        return value
    }else {
        return defaultValue
    }
}