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

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

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

王益

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.]

--
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 [hidden email].
To post to this group, send email to [hidden email].
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.
Reply | Threaded
Open this post in threaded view
|

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

Jeremy Tregunna
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

On Dec 18, 2014, at 19:54, 王益 <[hidden email]> wrote:

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.]

--
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 [hidden email].
To post to this group, send email to [hidden email].
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.

--
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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/B5581914-38A8-4C00-ABD1-01FA095400F9%40tregunna.ca.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

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

Marco S Hyman
In reply to this post by 王益
> 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 :)


--
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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/FC2E23BC-C52E-460D-8CB2-97631D1EA5E7%40snafu.org.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

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

Jeremy Pereira
In reply to this post by 王益
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?




> On 19 Dec 2014, at 01:54, 王益 <[hidden email]> wrote:
>
> 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:
>
> • In the first line, we removed all : and the ->.
> • 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.]
>
>
> --
> 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 [hidden email].
> To post to this group, send email to [hidden email].
> 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.

--
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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/DD012B75-3CA7-4CEB-9BDD-97022175FA59%40googlemail.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

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

Jens Alfke

On Dec 19, 2014, at 2:21 AM, Jeremy Pereira <[hidden email]> 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

--
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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/7230C3AB-0977-4C84-A60E-507A6487D7D6%40mooseyard.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

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

Brent Royal-Gordon
In reply to this post by 王益
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


On Thu, Dec 18, 2014 at 5:54 PM, 王益 <[hidden email]> wrote:

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.]

--
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 [hidden email].
To post to this group, send email to [hidden email].
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.

--
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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/1419029931672.a0339966%40Nodemailer.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

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

王益
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

On Fri, Dec 19, 2014 at 2:58 PM, Brent Royal-Gordon <[hidden email]> wrote:
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


On Thu, Dec 18, 2014 at 5:54 PM, 王益 <[hidden email]> wrote:

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.]

--
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 [hidden email].
To post to this group, send email to [hidden email].
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.

--
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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/CAFC3K3%2Bi9u36vqPCkT-k0UOLoOM3%2BgBH_L9Ory0Y2CuvKm%2Bavg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.