title: Shorthand for wrapping a swift variable in an optional?
body: <p>Swift allows us to use the shorthand notation <code>str!</code> to unwrap an optional. But what if we want to do the opposite?</p>
  
  <p>Say I have a variable:</p>
  
  <pre><code>var str = String() // String
  </code></pre>
  
  <p>Is there any shorthand notation to convert this to an optional (i.e. <code>String?</code> or <code>String!</code>)?</p>
  
  <p>(E.g. I want to do something like <code>var strOptional = ?(str)</code>.)</p>
  
  <p>Alternatively, if there is no shorthand for this notation, how can I convert it to an optional without explicitly mentioning its type (e.g. I don't want to mention <code>String</code>).</p>
  
  <p>In other words, I know that I can wrap a variable as an optional with any of these methods:</p>
  
  <pre><code>var strOptional = str as String?
  var strOptional: String? = str
  var strOptional = String?(str)
  </code></pre>
  
  <p>... but in each case, I must explicitly write <code>String</code>.</p>
  
  <p>I would rather write something like: <code>var strOptional = str as typeof?(str)</code>, if there is no shorthand syntax. (The advantage is that if the variable's type is frequently changed in the code base, it would be one less place to update.)</p>
  
  <hr>
  
  <p>As far as a real world example of where this would be useful, imagine I want to use an AVCaptureDevice and I use the following code:</p>
  
  <pre><code>let device = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)
  device.lockForConfiguration(nil)
  </code></pre>
  
  <p><code>lockForConfiguration()</code> will crash at runtime on a device that has no video camera, and the compiler won't warn me about it. The reason is that <code>defaultDeviceWithMediaType</code> may return <code>nil</code> according to the documentation<sup>[1]</sup>, yet it is defined to return a <code>AVCaptureDevice!</code>.</p>
  
  <p>To fix a faulty API like this, it would be nice to do something like:</p>
  
  <pre><code>let device = ?(AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo))
  </code></pre>
  
  <p>... to get a <code>AVCaptureDevice?</code>, and have the compiler catch any mistakes I might make.</p>
  
  <p>Currently, I must resort to the more verbose: </p>
  
  <pre><code>let device: AVCaptureDevice? = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)
  </code></pre>
  
  <hr>
  
  <p>Another example:</p>
  
  <p>In this case, I want to give a default value to my variable that is a string, but later on, I may want to assign it a <code>nil</code> value.</p>
  
  <pre><code>var myString = "Hi"
  // ...
  if (someCondition()) {
      myString = nil // Syntax error: myString is String
  }
  </code></pre>
  
  <p>Currently I have to resort to <code>var myString: String? = "Hi"</code> but something like <code>var myString = ?("Hi")</code> would be less verbose.</p>
  
  <hr>
  
  <p><sub>[1] If you open AVCaptureDevice.h, you will see the following documentation about the return value: "The default device with the given media type, or nil if no device with that media type exists."</sub></p>