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

Jim Malak Jim.Malak at beryle-lee.com
Mon Apr 4 13:38:14 CDT 2016

Thanks Jens,

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 at mooseyard.com>
Date: Monday, April 4, 2016 at 2:32 PM
To: Jim Malak <Jim.Malak at beryle-lee.com>
Cc: "swift-users at swift.org" <swift-users at 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 at swift.org<mailto: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.)

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20160404/8156a86b/attachment.html>

More information about the swift-users mailing list