[swift-evolution] [Idea] Wrap switch cases in curly braces

Peter Eddy peter.eddy at gmail.com
Sun Jul 10 13:25:47 CDT 2016


I appreciate the desire for consistency but I really don't like this
proposal. I agree with others who've said that it makes the code look heavy
and that the extra parens only obscure the actual code, at least when the
code is short. I don't think there is any way in which the parens make the
code clearer, they're just visual noise.

If we insist on consistency then shouldn't we also do away with implicit
returns from single-expression closures? After all this behavior is
inconsistent with named functions. And shouldn't closures also declare
their signature before their opening brace, rather than after, also to be
consistent with named functions?

All these breaks with consistency have the effect of making the language
easier and more pleasant to read, and they don't seem to be major stumbling
blocks for people learning Swift. I hate to see them go away.

- Peter

On Fri, Jul 8, 2016 at 1:58 AM, G B via swift-evolution <
swift-evolution at swift.org> wrote:

> I’d imagine it would be the same just without the colon and with the
> braces:
>
>
> enum MyEnum {
>     case IntEnum(Int)
>     case StringEnum(String)
> }
>
>
> var x=MyEnum.StringEnum("Hello")
>
> switch x {
>     case .IntEnum(let x) {
>         print(x)
>     }
>
>     case .StringEnum(let x) {
>         print(x)
>     }
>
> }
>
>
>
> Not much different than:
>
> if case .StringEnum(let v) = x {
>     print(v+" again")
> }
>
> except that the switch compares all case statements against x.
>
>
>
> > On Jul 7, 2016, at 10:37 PM, David Sweeris <davesweeris at mac.com> wrote:
> >
> > What would be the proposed syntax for cases with associated values?
> >
> > In general, +1 for simplifying the syntax, but I'm not sure it'd work
> well, given that enums take on a larger roll in Swift than they do in other
> languages. Or maybe it'd be fine... I'm just too tired to picture it in my
> head.
> >
> > - Dave Sweeris
> >
> >> On Jul 7, 2016, at 15:07, G B via swift-evolution <
> swift-evolution at swift.org> wrote:
> >>
> >> It has always seemed odd to me that `case`s use a colon as a delimiter
> rather than curly braces like everything else.  Is there a reason for this
> other than the legacy of C-like languages?
> >>
> >> If I wanted to write a series of branching `if` \ `else` statements I
> would do it like so:
> >>
> >> if x==0      { print(0) }
> >> else if x==1 { print (1) }
> >> else if x==2 { print(2) }
> >> else         { print("other”) }
> >>
> >> I believe all flow control is wrapped in curly braces, except for
> `case`s inside a `switch`:
> >>
> >> switch x {
> >> case 0: print(0)
> >> case 1: print(1)
> >> case 2: print(2)
> >> default: print("other")
> >> }
> >>
> >>
> >> I feel like this would be more consistent with the rest of the syntax:
> >>
> >> switch x {
> >> case 0 { print(0) }
> >> case 1 { print(1) }
> >> case 2 { print(2) }
> >> default { print("other”) }
> >> }
> >>
> >> The colon syntax evokes a label, but the modern, complex `case`
> statements in Swift don’t act much like labels.
> >> _______________________________________________
> >> swift-evolution mailing list
> >> swift-evolution at swift.org
> >> 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/20160710/6128a169/attachment.html>


More information about the swift-evolution mailing list