Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow for named blocks #62

Merged
merged 2 commits into from
Jun 30, 2017
Merged

Conversation

aem
Copy link

@aem aem commented Jun 30, 2017

Kotlin supports named blocks in cases where you want to return early or return from an inner block without returning a function, the syntax is as so:

fun printNFromList(list: List<*>, count: Int) {
  list.mapIndexed a@{ item, index ->
    if (index > count) return@a
    println(item)
  }
}
printNFromList(listOf(1, 2, 3), 2)

obviously that's contrived because return@map would suffice as well, but our code sometimes requires operations like .map { .map { } } which means return@method doesn't work, named blocks are required.

@shyiko
Copy link
Collaborator

shyiko commented Jun 30, 2017

Hi @aem. Did you mean prevLeaf?.node?.elementType == KtTokens.AT? (compilation failed)

@aem
Copy link
Author

aem commented Jun 30, 2017

whoops. yes. this is what i get for using vim 😞

@shyiko
Copy link
Collaborator

shyiko commented Jun 30, 2017

@aem no worries! :) Thank you for the PR! 🙇‍♂️

@shyiko shyiko merged commit 4ec4d7d into pinterest:master Jun 30, 2017
@aem
Copy link
Author

aem commented Jun 30, 2017

@shyiko always, never a fan of people opening issues on my projects without trying to do something about it, figured i'd be a good OSS citizen myself :)

@shyiko
Copy link
Collaborator

shyiko commented Jun 30, 2017

@aem we might have a problem here.

I was about to release a new version of ktlint but decided to check what Intellij formatter does when asked to reformat a block of code containing lambda expression with a label and, alas, a space is inserted between @ and {.

fun foo() {
    ints.forEach lit@{
        if (it == 0) return@lit
        print(it)
    }
}

becomes

fun foo() {
    ints.forEach lit@ {
        if (it == 0) return@lit
        print(it)
    }
}

(example taken from https://kotlinlang.org/docs/reference/returns.html)
In other words, previous behavior was in line with IDEA's built-in formatter (which pretty much reflects official code style).

@aem
Copy link
Author

aem commented Jun 30, 2017

interesting. looks like you're correct and we've been doing this differently in our codebase. i'm happy to update our styleguide to add the space before the brace if you want to back this change out

@shyiko
Copy link
Collaborator

shyiko commented Jun 30, 2017

@aem 🤝 yeah, I think it's better this way (staying as much IDEA-code-style-compatible as possible). Sorry about all the confusion.

@aem
Copy link
Author

aem commented Jun 30, 2017

no worries, should've checked docs/intellij's formatting first

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants