[swift-evolution] Shorthand Optional Notation proposal

Pranjal Satija pranjal.satija at icloud.com
Mon Jan 11 20:29:45 CST 2016


Hey there,

I have a proposal I’d like to ask you guys to look at before I try to submit it as a formal proposal on the GitHub repository. Before I write a complete draft with a detailed explanation of everything, I’d like to validate my idea a bit. Would it be beneficial to have a shorthand Optional operator in Swift? Currently, when initializing a variable with a non-optional value, the only way to make it optional is by using an explicit type, like this:

let myOptionalString: String? = “str”

This syntax can be cumbersome, especially for types with longer names. Now, there’s a force unwrap operator in Swift that allows you to put an optional value into a variable that’s expecting a non-optional type, like this:

let myForceUnwrappedValue = SomeInitializer()! 

So, to match, and to make things shorter, do you think there should be an operator that allows developers to achieve the same effect, but without an explicit type? Like this:

let myOptionalString = “str”?

Could you guys let me know what you think? Thanks for the help!


More information about the swift-evolution mailing list