[swift-evolution] Proposal Draft: Optional Upgrading Assignment.

Jerome ALVES j.alves at me.com
Thu Feb 4 09:45:57 CST 2016


Maybe allowing optional chaining here is the best option ?

let test = "Test" // String
let testOrNil = test? // String?

- Jérôme


> Le 4 févr. 2016 à 16:32, Paul Ossenbruggen via swift-evolution <swift-evolution at swift.org> a écrit :
> 
> Just sugar for that, plus there is symmetry to this proposal, it is more discoverable, and it is more concise.
> 
> 
>> On Feb 4, 2016, at 1:57 AM, Taras Zakharko <taras.zakharko at uzh.ch <mailto:taras.zakharko at uzh.ch>> wrote:
>> 
>> -1 from me
>> 
>>   let x = Optional(“Some Value”) 
>> 
>> does the job just fine, is very clean,  and I don’t see any reason to shorten it. 
>> 
>> — Taras
>> 
>> 
>>> On 04 Feb 2016, at 10:33, Allen Ding via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>>> 
>>> On Thu, Feb 4, 2016 at 4:28 PM, Paul Ossenbruggen via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>>> 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.
>>> 
>>> Any use case examples? I've been trying to figure out why is this needs fixing (I don't even see it as a problem).
>>> 
>>> 
>>> ## 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 as var and let that currently exist in the language. 
>>> 
>>> The advantages: this is easier to read, more compact, and lets type inference determine the type. 
>>> 
>>> If let and 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 the if let, (guard let) context because if let unwraps the value, so this would be an error. 
>>> 
>>> This should not conflict with pattern matching as that requires the case keyword. 
>>> 
>>> 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. 
>>> 
>>> 
>>> 
>>> _______________________________________________
>>> swift-evolution mailing list
>>> swift-evolution at swift.org <mailto:swift-evolution at swift.org>
>>> https://lists.swift.org/mailman/listinfo/swift-evolution <https://lists.swift.org/mailman/listinfo/swift-evolution>
>>> 
>>> 
>>> _______________________________________________
>>> swift-evolution mailing list
>>> swift-evolution at swift.org <mailto:swift-evolution at swift.org>
>>> https://lists.swift.org/mailman/listinfo/swift-evolution <https://lists.swift.org/mailman/listinfo/swift-evolution>
> 
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160204/9eec5138/attachment.html>


More information about the swift-evolution mailing list