Code Monkey home page Code Monkey logo

Comments (9)

hollance avatar hollance commented on June 11, 2024

My vote is always for the simplest thing possible.

How should we format closures with parameters? A Sprite Kit example:

return SKAction.customActionWithDuration(effect.duration, actionBlock:
  {(node: SKNode!, elapsedTime: CGFloat) in
    // the actual code goes here
  })

I'm not very happy with this syntax (especially the line with the closure's parameters) but it's the least ugly thing I could come up with...

from swift-style-guide.

ColinEberhardt avatar ColinEberhardt commented on June 11, 2024

My vote is always for the simplest thing possible.

OK, sounds good

How should we format closures with parameters? A Sprite Kit example:

erm ... see above, the simplest form does not have explicit parameters :-P

from swift-style-guide.

hollance avatar hollance commented on June 11, 2024

Like this you mean?

return SKAction.customActionWithDuration(effect.duration, actionBlock: {(node, elapsedTime) in
  // the actual code goes here?
})

(For some reason this didn't compile for me earlier. It works fine now... Xcode bug, no doubt.)

Anyway, my question was more along the lines of, where do we put the parameters: on the same line, on the next line?

from swift-style-guide.

ColinEberhardt avatar ColinEberhardt commented on June 11, 2024

If using parameter names I like the idea of having the on a separate line, as it reflects the way that method are formatted.

from swift-style-guide.

hollance avatar hollance commented on June 11, 2024

Of course, this will also work:

return SKAction.customActionWithDuration(effect.duration) {(node, elapsedTime) in 
  // the actual code goes here
}

from swift-style-guide.

ColinEberhardt avatar ColinEberhardt commented on June 11, 2024

Of course, this will also work

Yes, that's the trailing closure syntax, and I really don't like it! Perhaps it is better for multi-line closures, which removes the need to end with })

from swift-style-guide.

 avatar commented on June 11, 2024

This one is tricky. There are so many ways to do it. Again, in the session videos they seem to be bragging about cool stuff like $0, but I tend to think those types of variables make things less readable. I use them in scripts because I don't have a variable name, but otherwise, why not name it?

Of course, str.sort(>) is the preferred method for that particular call. You shouldn't define a new closure for one that already exists.

But in cases of our own closures, I am not so sure. This will probably come down to preference for a while until I've written and read more Swift code.

My first guess as to my preference is

a.func({ a, b in return doSomething(a, b) })

I think that is how most of the examples I've see have been.

Also, in some sessions they have examples like this:

a.func { a, b in
doSomething(a, b)
}

Not sure how I feel about it, but it does separate the parameters from the code a bit

--------- Original Message --------- Subject: [swift-style-guide] Discussion: Closure syntax (#4)
From: "ColinEberhardt" [email protected]
Date: 6/10/14 1:41 am
To: "raywenderlich/swift-style-guide" [email protected]

The Swift closure syntax is incredibly flexible. These are all semantically the same thing:
str.sort({(one: String, two: String) -> Bool in return one.compare(two) < 0}) str.sort({(one, two) in return one.compare(two) < 0}) // return type inferred str.sort({one, two in return one.compare(two) < 0}) // no need for brackets str.sort({$0.compare($1) < 0}) // shorthand variable and implicit return str.sort() {$0.compare($1) < 0} // sort has a single parameter, so closure can pop outside str.sort {$0.compare($1) < 0} // no need for brackets str.sort(>) // ok, not a closure, but > has the correct signature. Awesome! Should we recommend that people use the shortest form possible unless it seriously impacts readability?

Reply to this email directly or view it on GitHub.

from swift-style-guide.

jackwu95 avatar jackwu95 commented on June 11, 2024

I think first of all, we should always be using trailing closure syntax where possible. It seems like one of the most loved features of Swift by Apple.

I also don't like implicit returns, so I think return should be added whenever the closure expects a return value.

Other than that,

a.func { a, b in
doSomething(a, b)
}

looks pretty good!

from swift-style-guide.

rwenderlich avatar rwenderlich commented on June 11, 2024

+1 for the "simplest possible format other than numbered variables, with parameter names":

a.func { a, b in
doSomething(a, b)
}

from swift-style-guide.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.