[swift-evolution] Shorthand Optional Notation proposal

肇鑫 owenzx at gmail.com
Mon Jan 11 22:40:36 CST 2016


You can always use

let myOptionalString = Optional(“str”)

instead.

Or use the code below

postfix operator / { }

postfix func / (value:String) -> String? {
    return Optional(value)
}

let string = "str"/
print(string) // Optional("str")

I try to use ? to replace /. It doesn't work. As the compiler try to think
? { } as a tuple instead of an operator.

I also tried to replace String with T. It seams that you can not use
operator function with generics.

zhaoxin

On Tue, Jan 12, 2016 at 10:29 AM, Pranjal Satija via swift-evolution <
swift-evolution at swift.org> wrote:

> 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!
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
>



-- 

Owen Zhao
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160112/ed3ebc08/attachment.html>


More information about the swift-evolution mailing list