title: Providing a default value for an Optional in Swift?
body: <p>The idiom for dealing with optionals in Swift seems excessively verbose, if all you want to do is provide a default value in the case where it's nil:</p>
  
  <pre><code>if let value = optionalValue {
      // do something with 'value'
  } else {
      // do the same thing with your default value
  }
  </code></pre>
  
  <p>which involves needlessly duplicating code, or</p>
  
  <pre><code>var unwrappedValue
  if let value = optionalValue {
      unwrappedValue = value
  } else {
      unwrappedValue = defaultValue
  }
  </code></pre>
  
  <p>which requires <code>unwrappedValue</code> not be a constant.</p>
  
  <p>Scala's Option monad (which is basically the same idea as Swift's Optional) has the method <code>getOrElse</code> for this purpose:</p>
  
  <pre><code>val myValue = optionalValue.getOrElse(defaultValue)
  </code></pre>
  
  <p>Am I missing something? Does Swift have a compact way of doing that already? Or, failing that, is it possible to define <code>getOrElse</code> in an extension for Optional?</p>