[swift-evolution] Proposal Draft: Optional Upgrading Assignment.
Radosław Pietruszewski
radexpl at gmail.com
Thu Feb 4 02:33:48 CST 2016
What’s your use case for this? I don’t find myself doing this very often.
And when I do, Optional(x) does the job.
> Draft Proposal feedback welcome.
>
> ## Introduction
>
> Currently to make a optional from a value the type name must be repeated:
>
> let x = “Some Value”
> let y : String? = x
>
> This takes away some of the advantage of type inference because the very next line I must specify the type.
>
> ## Proposal
>
> I propose the following syntax:
>
> let y? = x
>
> This binds a new optional y which wraps the same value of x. It follows the same conventions asvarandletthat currently exist in the language.
>
> The advantages: this is easier to read, more compact, and lets type inference determine the type.
>
> If letand other conditionals would not support this syntax and optionals of optionals is not supported. So let x?? = y would not result in an optional optional. Also if y was an optional it would not let you upgrade that.
>
> ## Detailed Design
>
> The grammar:
>
> let optionalValue? = value
>
> This would not make sense in theif let, (guardlet)context becauseif letunwraps the value, so this would be an error.
>
> This should not conflict with pattern matching as that requires thecasekeyword.
>
> Wrapping optional in an optional is an error.
>
> ## Impact on existing code
>
> The current syntax will continue to exist so it will have no impact on existing code.
>
> ## Alternatives Considered
>
> None.
>
>
>
>
>
— Radek
More information about the swift-evolution
mailing list