My background going into Swift was everything but JavaScript. One thing I often wondered about (usually every time I forgot to type it) is what purpose does requiring the keyword “func” serve when defining or decaling a function in Swift. I am not a compiler expert but intuitively it would seem that the function signature could be readily recognized by the complier and people alike with the presences of the ()’s at the end.
My background going into Swift was everything but JavaScript.
Have you used Ruby or Python, both of which use a “def” keyword before a definition? And come to think of it, I think most non-C-derived languages use some sort of keyword before a function definition. Just to name two language families, LISP uses “def” and Pascal uses “function” or “procedure”.
So C (and C++ and Java and C#…) may be the oddballs here in not using a keyword, along with some functional languages.
One thing I often wondered about (usually every time I forgot to type it) is what purpose does requiring the keyword “func” serve when defining or decaling a function in Swift. I am not a compiler expert but intuitively it would seem that the function signature could be readily recognized by the complier and people alike with the presences of the ()’s at the end.
I can’t speak for the details of Swift’s language grammar. I do know that adding not-strictly-necessary redundancies like this to a grammar is very useful for helping the parser understand the intended meaning of a syntax error, and thereby producing a meaningful error message. (This is one of the reasons C++ error messages can be notoriously unhelpful: the syntax is so complex with so many possible ambiguities, that the error message may refer to a language construct that you weren’t even intending to use.)
—Jens
···
On Apr 4, 2016, at 7:25 AM, Jim Malak via swift-users <swift-users@swift.org> wrote:
My main languages are C#, c, obj-c and now Swift. You can appreciate my point of view. I’ll just assume that adding “func” contributes towards the common good.
Thanks for responding.
- Jim
···
From: Jens Alfke <jens@mooseyard.com>
Date: Monday, April 4, 2016 at 2:32 PM
To: Jim Malak <Jim.Malak@beryle-lee.com>
Cc: "swift-users@swift.org" <swift-users@swift.org>
Subject: Re: [swift-users] Question out of curiosity: why "func"
On Apr 4, 2016, at 7:25 AM, Jim Malak via swift-users <swift-users@swift.org<mailto:swift-users@swift.org>> wrote:
My background going into Swift was everything but JavaScript.
Have you used Ruby or Python, both of which use a “def” keyword before a definition? And come to think of it, I think most non-C-derived languages use some sort of keyword before a function definition. Just to name two language families, LISP uses “def” and Pascal uses “function” or “procedure”.
So C (and C++ and Java and C#…) may be the oddballs here in not using a keyword, along with some functional languages.
One thing I often wondered about (usually every time I forgot to type it) is what purpose does requiring the keyword “func” serve when defining or decaling a function in Swift. I am not a compiler expert but intuitively it would seem that the function signature could be readily recognized by the complier and people alike with the presences of the ()’s at the end.
I can’t speak for the details of Swift’s language grammar. I do know that adding not-strictly-necessary redundancies like this to a grammar is very useful for helping the parser understand the intended meaning of a syntax error, and thereby producing a meaningful error message. (This is one of the reasons C++ error messages can be notoriously unhelpful: the syntax is so complex with so many possible ambiguities, that the error message may refer to a language construct that you weren’t even intending to use.)
Swift’s grammar follows a very regular structure, where declarations are introduced with a keyword. This structure has a number of advantages when parsing. For example, it allows “modifiers” like “mutating” and “public” to be context sensitive identifiers instead of themselves being keywords. It also helps with more subtle things like error recovery in the face of invalid code that produces parsing errors.
-Chris
···
On Apr 4, 2016, at 7:25 AM, Jim Malak via swift-users <swift-users@swift.org> wrote:
My background going into Swift was everything but JavaScript. One thing I often wondered about (usually every time I forgot to type it) is what purpose does requiring the keyword “func” serve when defining or decaling a function in Swift. I am not a compiler expert but intuitively it would seem that the function signature could be readily recognized by the complier and people alike with the presences of the ()’s at the end.
_____________________________
From: David Turnbull <dturnbull@gmail.com<mailto:dturnbull@gmail.com>>
Sent: Tuesday, April 5, 2016 2:03 AM
Subject: Re: [swift-users] Question out of curiosity: why "func"
To: Jim Malak <jim.malak@beryle-lee.com<mailto:jim.malak@beryle-lee.com>>
Cc: <swift-users@swift.org<mailto:swift-users@swift.org>>
On Mon, Apr 4, 2016 at 7:25 AM, Jim Malak via swift-users <swift-users@swift.org<mailto:swift-users@swift.org>> wrote:
what purpose does requiring the keyword "func" serve when defining or decaling a function in Swift.