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
CodeBlock.endFlowControl shouldn't insert a new line by default #1330
Comments
I think the counterpoint is when you're writing it as statements in a body so I'm not sure omitting is a great default either. In Kotlin it actually feels like we should model more constructs like control flow as a type which you can either emit as a statement or part of an expression. Another one that comes to mind in this space is that you cannot use beginControlFlow for a trailing lambda with a named argument because it handles emitting the curly. |
I think that
This actually is supported! kotlinpoet/kotlinpoet/src/test/java/com/squareup/kotlinpoet/CodeBlockTest.kt Lines 454 to 468 in a1163da
|
Tough to pull this off without breaking like 100% of users though. Might be time to rework the whole "code" part of the API. |
Another example where it's a problem that endControlFlow inserts a newline. If I want to generate this (Http4K) code: public fun route(): ContractRoute {
// Route
return "/items" meta {
summary = "Summary."
description = "Description."
returning(OK to "Success.")
} bindContract GET to ::handler
} This: FunSpec.builder("route").apply {
returns(ClassName("org.http4k.contract", "ContractRoute"))
addComment("Route")
beginControlFlow("return %S meta {", "/items")
addStatement("summary = %S", "Summary.")
addStatement("description = %S", "Description.")
addStatement("returning(OK to %S)", "Success.")
endControlFlow().addStatement("bindContract GET to ::handler")
}.build() Actually generates this code with a newline before public fun route(): ContractRoute {
// Route
return "/items" meta {
summary = "Summary."
description = "Description."
returning(OK to "Success.")
}
bindContract GET to ::handler
} |
That is not control flow. It's a multiline single statement. |
Probably a relict of JavaPoet where a control flow would almost certainly be followed by new line. With Kotlin, new line by default makes code like below look bad:
The text was updated successfully, but these errors were encountered: