Not sure, if there is a better channel, but I'd li...
# intellij
h
Not sure, if there is a better channel, but I'd like to argue the style default to add whitespace before if, when and for. Why is that? Nowhere else in kotlin is a whitespace used before a round bracket. The difference between keyword and function call is IMHO so fuzzy, because users can and will express very similar constructs enabled by https://kotlinlang.org/docs/lambdas.html#passing-trailing-lambdas which is reformatted inconsistently (using default setting):
Copy code
fun myIf(condition: Boolean, block: () -> Any) {
    block()
}

fun main() {
    if (true) {
        println("foo")
    }

    myIf(true) {
        println("bar")
    }
}
And yes, I know how to change the default. But I'd rather like to see consistency in the formatting, and would love to upvote some kepp/tickeet to change the default.
2
e
Just to be clear, are you advocating
if(someCondition)
over
if (someCondition)
?
h
Yes I do. See my edited example (slack was posting it before I was done).
e
keywords are special. their names can't be overridden or shadowed;
if
can chain with
else if
which
myIf
can't; looping constructs can
break;continue
which user code can't,
when
can't be replicated at all. basically I'm negative on changing the style, it's fine that different things look different.
7
h
imho not true @ephemient. For sure we can express quite a few of your mentioned keyword constructs with functions. E.g. if-else chaining, which we can easily express with an infix extension:
Copy code
fun myIf(condition: Boolean, block: () -> Any) {
    block()
}

infix fun Any.myElse(block: () -> Any) {
    block()
}

fun main() {
    if (true) {
        println("foo")
    }

    myIf(true) {
        println("bar")
    } myElse {
        println("bar")
    }
}
break, and continue do not use brackets, so I don't think they relate to my suggestion. Indeed
when
is special. Because we can do so, formatting experience lacks consistency with the current default style.
e
else if
, with a space, is impossible to replicate
e
if (true) println("foo") else println("bar")
as well, I think?
h
@Emil Kantis no this can be expressed with function calls easily
Copy code
private infix fun Any.myElse(println: Unit) {}

private infix fun Any.println(s: String) {}

fun myIf(b: Boolean): Any {}


fun main() {
    if (true) println("foo") else println("bar")
    
    myIf (true) println("foo") myElse println("bar")
}
@ephemient else if -->
Copy code
private infix fun Any.myElse(smthg: Boolean) {}

private infix fun Any.println(s: String) {}

fun myIf(b: Boolean): Any  = 23


fun main() {
    if (true) println("foo") else println("bar")

    val myOtherIf = true

    myIf(true) println ("foo") myElse myOtherIf println ("bar")
}
Admittedly a nonsense example, but that's not my point. I'd think that the great majority of users will just perceive inconsistency here: Keyword and functional constructs can realize the similar code constructs but are formatted differently.
e
Obviously you can chain the syntax, but the control flow is not there. Plus it's a bit smelly to allow
myElse
for
Any
, should be confined to work with the if..
h
I'd did not say it's pretty or good design, but it's a valid construct that is formatted inconsistently depending on if keywords or functions are being used. This semantic difference will be clear to experts but new users will most likely just notice the inconsistency of spacing in front of round brackets.