mirror of
https://github.com/samsonjs/samhuri.net.git
synced 2026-06-26 04:59:35 +00:00
Existing Tags fields were messy (mixed case, duplicates, lots of one-off tags) and 28 posts had none at all. Normalized casing, deduped, mapped common synonyms (textmate/xcode/git -> tools, os x variants -> mac-os-x, etc) onto a canonical set, and added 1-2 canonical tags to every previously untagged post so most posts now carry at least one useful tag. Specific extra tags were kept alongside the canonical ones rather than stripped. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1.1 KiB
1.1 KiB
| Author | Title | Date | Timestamp | Tags |
|---|---|---|---|---|
| Sami Samhuri | A nil-coalescing alternative for Swift | 6th October, 2017 | 2017-10-06T14:20:13-07:00 | ios, swift |
Swift compile times leave something to be desired and a common culprit is the affectionately-named nil-coalescing operator. A small extension to Optional can improve this without sacrificing a lot of readability.
extension Optional {
func or(_ defaultValue: Wrapped) -> Wrapped {
switch self {
case .none: return defaultValue
case let .some(value): return value
}
}
}
And you use it like so:
let dict: [String : String] = [:]
let maybeString = dict["not here"]
print("the string is: \(maybeString.or("default"))")
let otherString = dict["not here"].or("something else")
I'm sure someone else has come up with this already but I haven't seen it yet.