swift-book icon indicating copy to clipboard operation
swift-book copied to clipboard

Error in Grammar of a function type

Open tadbyt opened this issue 1 year ago • 0 comments

Location

https://docs.swift.org/swift-book/documentation/the-swift-programming-language/functions#Variadic-Parameters https://docs.swift.org/swift-book/documentation/the-swift-programming-language/types#Function-Type https://docs.swift.org/swift-book/documentation/the-swift-programming-language/summaryofthegrammar#Types

Description

Per the Variadic Parameters subsection:

A function can have multiple variadic parameters.

Per the Function Type subsection:

A function type can have variadic parameters in its parameter type. Syntactically, a variadic parameter consists of a base type name followed immediately by three dots (...), as in Int....

However, in the Grammar of a function type, the three dots can only apply to the type of the final parameter:

function-type-argument-clause → ( function-type-argument-list ...? )

Correction

The grammar should be revised as follows with adjustments to both the function-type-argument-clause and function-type-argument productions:

Grammar of a function type

function-type → attributes? function-type-argument-clause async? throws? -> type function-type-argument-clause → ( ) function-type-argument-clause → ( function-type-argument-list ) function-type-argument-list → function-type-argument | function-type-argument , function-type-argument-list function-type-argument → attributes? parameter-modifier? type ...? function-type-argument → argument-label type-annotation ...? argument-label → identifier

This doesn't put the following restriction given in the Variadic Parameters subsection, but it isn't clear that that needs to be covered by the formal grammar per se. If it does need to be covered by the grammar, then the correction is a bit more extensive.

The first parameter that comes after a variadic parameter must have an argument label. The argument label makes it unambiguous which arguments are passed to the variadic parameter and which arguments are passed to the parameters that come after the variadic parameter.

Note: This grammar is given in two locations, at the end of the Function Type subsection in the Types section and in the Types subsection of the Summary of the Grammar section.

tadbyt avatar Jan 22 '24 06:01 tadbyt