Last active
June 1, 2022 08:58
-
-
Save danielctull/40e4fcc5fc3f70980ea582a1cced4e7d to your computer and use it in GitHub Desktop.
I think I found the perfect improvement for force unwrapping in swift.
This file contains 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
// EXAMPLE | |
let string: String? = "Hello World" // String? | |
let unwrappedString = string.really.srsly.itsfine.honestly.aaarggh // String | |
print(unwrappedString) | |
// IMPLEMENTATION | |
extension Optional { | |
var really: Really<Wrapped> { | |
Really(srsly: Srsly(itsfine: ItsFine(honestly: Honestly(aaarggh: { self! })))) | |
} | |
} | |
struct Really<Value> { | |
let srsly: Srsly<Value> | |
} | |
struct Srsly<Value> { | |
let itsfine: ItsFine<Value> | |
} | |
struct ItsFine<Value> { | |
let honestly: Honestly<Value> | |
} | |
struct Honestly<Value> { | |
var aaarggh: Value { getAaarggh() } | |
private let getAaarggh: () -> Value | |
init(aaarggh: @escaping () -> Value) { | |
getAaarggh = aaarggh | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment