[swift-evolution] guard let x = x

Haravikk swift-evolution at haravikk.me
Tue Nov 1 03:17:16 CDT 2016


> On 1 Nov 2016, at 01:37, Joe Groff via swift-evolution <swift-evolution at swift.org> wrote:
> 
> Sorry for piling onto the bikeshed. We do already have a notation for testing that an Optional isn't nil, `x != nil`. We could theoretically bless `<decl ref> != nil` as a statement condition to also unwrap the referenced declaration in the scope guarded by the condition. (`<decl ref> is T` could similarly rebind a declaration as the cast type.)
> 
> -Joe

Was about the say the same thing; this is basically the same idea as type narrowing, which is something I think that Swift still really needs, and can be used to solve this problem without the need for new syntax, enabling things like:

	if (a is Foo) { a.someMethodSpecificToFoo() }
	if (a != nil) { a.someMethodWithoutUnwrapping() }

While I've often suggested keywords to solve other problems, I'm not sure if we really need one to solve this use-case!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20161101/226d6bb6/attachment.html>


More information about the swift-evolution mailing list