https://kotlinlang.org logo
Title
l

Luke

07/17/2019, 8:53 PM
Out of curiosity, is there a reason why the parentheses of a function without parameters are not optional?
s

Shawn

07/17/2019, 8:57 PM
w/o parens it looks like a property, potentially
Kotlin in general tries to avoid ambiguity
imo function calls should be immediately apparent to the reader
3
r

Ruckus

07/17/2019, 9:04 PM
Also, you could potentially run into ambiguity with the
invoke
operator.
fun test(op: Thing.() ->) {
    op  // Does this reference the value, or invoke the lambda?
}
👆 2
s

streetsofboston

07/17/2019, 9:05 PM
You can create some confusion by creating this extension function
operator fun Type1.invoke(block: () -> Type2) : Type3
🙂
For
fun test(op: Thing.() -> Unit) {
    op  // Does this reference the value, or invoke the lambda?
}
the
op
refers to the lambda value; you don’t invoke it.
someThing.op()
invokes it.
l

Luke

07/18/2019, 1:39 AM
No, I didn’t mean when calling the function. Yes the call should absolutely use empty parentheses. I’m referring to the function definition. What if instead of defining
fun foo() {...}
, you could write
fun foo {...}
. I don’t see a confusion or ambiguity here
s

streetsofboston

07/18/2019, 3:21 AM
Ah! That would be indeed a little less typing and because of the
fun
keyword, it won't be ambiguous. But somehow it looks odd/off to me....
l

Luke

07/18/2019, 1:30 PM
Yeah, I got that feeling too, but it might just be a out of habbit. Like we’re so used to see functions declared with them that removing them feels odd but we could get used to that new way
Anyway, I’m just thinking out loud here