I was recently reading the SPL book for Swift 5 on iBooks. In the reference section about enum
types, a case with a payload has said payload defined by a tuple type. But that section needs to be tweaked, because a payload may have exactly one component, while the tuple type syntax explicitly forbids tuples with exactly one component.
I don't remember exactly where those issues go, but IIRC you should open a radar for the issue.
Thanks! That's a good point — I was thinking that it uses the tuple type because of the grammatical similarity, but it looks like the grammar for tuple types does correctly rule out single element tuples: Types — The Swift Programming Language (Swift 5.7)
I've opened radar #49906528 to track the issue.
My thoughts on grammar changes could be:
-
In the “Tuple Type” section, add:
- empty-tuple-type -> ( )
- single-element-tuple-type -> ( tuple-type-element )
- multi-element-tuple-type -> ( tuple-type-element , tuple-type-element-list )
-
Change the following production:
- tuple-type -> empty-tuple-type | multi-element-tuple-type
-
In the “Enumeration Declaration” section add:
- payload-tuple-type -> single-element-tuple-type | multi-element-tuple-type
-
Change the following production:
- union-style-enum-case -> enum-case-name payload-tuple-type_opt
Edit: I forgot to mention that, AFAIK, empty payloads are banned for case
s; you must use a singular case when there’s no payload. The grammar productions above reflect this.
The grammar production for cases is now the same as the grammar production for subscript indexes (both based on function parameters). They shouldn't really be based on the tuple grammar at all anymore, which makes sense because there's no such thing as a "single element tuple" anymore.