[swift-evolution] Allow let binding of non-optionals

Gwendal Roué gwendal.roue at gmail.com
Thu Jan 7 13:07:41 CST 2016


> Le 7 janv. 2016 à 20:02, Joe Groff <jgroff at apple.com> a écrit :
> 
> 
>> On Jan 7, 2016, at 10:59 AM, Jacob Bandes-Storch via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>> 
>>         do let value2 = somethingNonOptional()
>> 
> 
> 'case let' already works like this, by declaring a pattern 'let a' that unconditionally matches anything.
> 
> -Joe

Well, the compiler complains, and does not provide any nice suggestion.

I, and most code I’ve seen so far (including StackOverflow), solve this issue with an extra variable declared in the outer scope:

	// error
	if let x = value...
	
	// OK
	let x = value
	if …

Remember the double role of `if let`: 1. unwrap the optional, 2. define a new variable, scoped to the `if` statement. We have 1, but not 2, and this is the gist of this thread.

Gwendal

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160107/6ed82f62/attachment.html>


More information about the swift-evolution mailing list