[swift-users] Question out of curiosity: why "func"

Jens Alfke jens at mooseyard.com
Mon Apr 4 13:32:48 CDT 2016


> On Apr 4, 2016, at 7:25 AM, Jim Malak via swift-users <swift-users at 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.)

—Jens
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20160404/2189fd63/attachment.html>


More information about the swift-users mailing list