Is it possible to make the syntax of function definition more concise?

37 views
Skip to first unread message

王益

unread,
Dec 18, 2014, 8:54:44 PM12/18/14
to swift-l...@googlegroups.com

I saw the following Swift example on this page:

func jediGreet(name: String, ability: String) -> (farewell: String, words: String) {
  return ("Good bye, \(name)", " May the \(ability) be with you.")
}

An equivalent version in Go is as follows:

func jediGreet(name string, ability string) (farewell string, words string) {
  return "Good bye, " + name, " May the " + ability + " be with you."
}

Notable differences include:

  1. In the first line, we removed all : and the ->.
  2. In the second line, we removed ( and ).

This Q/A explained the reason that Swift needs :, but still, it looks like that -> and ( ) are redundant.

Do they really unnecessary? If so, is it because of some intellectual issues so that Apple cannot adopt a concise syntax as Go already has?

[Yes, I noticed that the second line of the Go version is actually longer than the second line of the Swift version. Thanks to the \(param) syntax of Swift. I love it.]

Jeremy Tregunna

unread,
Dec 18, 2014, 9:22:28 PM12/18/14
to 王益, swift-l...@googlegroups.com
Remember that go'a grammar was designed for a computer to parse very fast while not being totally foreign. Swifts syntax appears to have been designed in such a way to be more approachable. At least, that's how I see it. 

Sent from my iPhone
--
You received this message because you are subscribed to the Google Groups "Swift Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to swift-languag...@googlegroups.com.
To post to this group, send email to swift-l...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/8733225a-127f-48c2-97dd-50749bd52b83%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Marco S Hyman

unread,
Dec 19, 2014, 12:01:15 AM12/19/14
to 王益, swift-l...@googlegroups.com
> func jediGreet(name: String, ability: String) -> (farewell: String, words: String) {
> return ("Good bye, \(name)", " May the \(ability) be with you.")
> }
>
> This Q/A explained the reason that Swift needs :, but still, it looks like that -> and ( ) are redundant.

The Second pair of parens are needed because the function is returning a
tuple. They would not be needed if returning a string, for example.
Also, the return names are not necessary in many (most?) instances.

A simpler version for when a return tuple isn't needed.

func jediGreet(name: String, ability: String) -> String {
return "Good bye, \(name), May the \(ability) be with you."
}

I haven't poured through the grammar to know if the -> is absolutely necessary.
I like it. It helps people understand what is going on which is often more
important than helping the computer parse the language. But then I always
liked Knuth's literate programming model, too :)


Jeremy Pereira

unread,
Dec 19, 2014, 5:21:43 AM12/19/14
to 王益, swift-l...@googlegroups.com
The Swift way is vastly more readable for the addition of those few extra characters, especially when you consider that function definitions can be type annotations for parameters in other function definitions. Also, be aware of currying in Swift

Why the obsession with the fewest possible keystrokes?

Jens Alfke

unread,
Dec 19, 2014, 11:49:03 AM12/19/14
to Jeremy Pereira, 王益, swift-l...@googlegroups.com

On Dec 19, 2014, at 2:21 AM, Jeremy Pereira <jeremy.j...@googlemail.com> wrote:

Why the obsession with the fewest possible keystrokes?

+1. If you want the shortest possible program, learn APL. Then have fun trying to read it the next day :)

FWIW, I've been coding in Go for two years, and I prefer Swift's function syntax because it's more readable.

—Jens

Brent Royal-Gordon

unread,
Dec 19, 2014, 5:58:55 PM12/19/14
to 王益, swift-l...@googlegroups.com
The “->” is used all throughout Swift to specify the types of functions. The parameters always go on its left and the return values on its right. Apple could probably have omitted it when declaring a function, but it’s harder to remove in other parts of Swift that involve function types:

    var changer: String -> (String?, NSError?)
    // A variable containing a function
    
    func changeWithCompletion(completion: NSError? -> Void)
    // Takes a function as a parameter
    
    class func makePrefixer(prefix: String) -> String -> (String?, NSError?)
    // Returns a function in its return value

It’s also worth noting that Swift’s currying syntax would have to go:

    class func makePrefixer(prefix: String)(str: String) -> (String?, NSError?)
    // Another way of writing the method above

Ultimately, though, this is a stylistic judgement. Swift’s designers thought that the “ParamType -> ReturnType” syntax was simple and elegant, and wanted to use it anywhere in Swift that you declared the type of a function. Go’s designers thought that “func (ParamTypes) ReturnTypes” was just fine. You could, perhaps, link this to the fact that modern Cocoa APIs use blocks heavily, or to members of Apple’s longstanding compiler team being influenced by Haskell, or to all sorts of other things, but ultimately it’s just an aesthetic choice that the two teams made differently.


Brent Royal-Gordon
Sent from Mailbox


--

王益

unread,
Dec 19, 2014, 6:59:16 PM12/19/14
to Brent Royal-Gordon, swift-l...@googlegroups.com
Thanks Brent.

The second example makes me think that in order to be consistent, there should be either `func` or `->`.  For example, either

    changeWithComplete(completion: NSError -> Void) -> Void

or

    func changeWithComplete(completion: func(NSError) Void)

Obviously, the later is better, because we can save "Void" when the function returns nothing:

    func changeWithComplete(completion: func(NSError))

Yi
Reply all
Reply to author
Forward
0 new messages