[swift-users] Switch based on let

Dan Loewenherz dan at lionheartsw.com
Fri Jul 8 10:49:11 CDT 2016


On Fri, Jul 8, 2016 at 10:34 AM, Roth Michaels via swift-users <
swift-users at swift.org> wrote:

> On Fri, Jul 08 2016 at 11:11:04 AM, Nate Birkholz via swift-users <
> swift-users at swift.org> wrote:
> > This gives an error, expecting a colon (:) after object on every case.
> >
> > I wanted to be sure I wasn't missing something in my syntax (nor some
> > obvious-to-others reason this isn't supported) before going to swift
> > evolution.
>
> There is a bug filed to improve this error message:
> https://bugs.swift.org/browse/SR-2022
>
> On Fri, Jul 08 2016 at 11:15:48 AM, Dan Loewenherz via swift-users <
> swift-users at swift.org> wrote:
> > To my knowledge, you can’t do exactly what you’re trying to do
>
> Here is how:
>
> ```Swift
>
> for object in objects {
>     switch object {
>     case let object as Subclass1:
>         doSomethingWith(object)
>     case let object as Subclass2:
>         doSomethingWith(object)
>     case let object as Subclass3:
>         doSomethingWith(object)
>     default:
>         break
>     }
> }
>

Beautiful, thanks Roth! This is much nicer. :)

Dan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20160708/76b2bbf4/attachment.html>


More information about the swift-users mailing list