A tiny syntactic-sugar trick around Swift Optionals

Not a long post this time, Just a tiny snippet of code I found useful in some scenarios :)

I noticed I’ve been seeing a lot of code similar to the following:

guard let myObject = MyObject(dependency: xyz) else { 
print("This thing is nil")

So i’ve decided to write a tiny extension to make this a bit nicer. 
With the code provided at the end of this post, this would look something like this:

MyObject(dependency: xyz)
.then { doSomethingWithObject($0) }
.otherwise { print("This thing is nil") }

This is much more concicse in many cases (especially, for example, if you just want to do something with the object and don’t care about the nullability of it in that case)

It’s a pretty simple snippet, but thought it would be nice to share. Have fun and would love to hear any suggstion you have for it!