why do i have to type ```val x = ::main``` instead...
# language-proposals
l
why do i have to type
Copy code
val x = ::main
instead of just
Copy code
val x = main
to store a function in a variable?
a
I'm guessing because
main
could be a property, so
main
would execute its getter but I'm no expert 😛
l
well im specifically asking about the case where
main
is a function, not a property
m
As per Arek, it's what Kotlin wants. Clarity over conciseness. Deterministic rather than variable outcomes. There is no chance of confusion using
::main
. If there is no function called
main
, then it's wrong. For `main', again, no confusion. It will look for a property/variable with that name. If you allow what you propose, it's not clear to others what your intent is, and what happens if you have a
fun main
, use
main
and someone adds a
main
variable/property? All of a sudden this code doesn't work. If you're lucky, it will be at compile time, but it's just as likely to be at runtime.
l
ooh i didnt know you can have a function and a property with the same name at the same time
m
AFAIK you can, yes. Hence the strict requirement.
a
l
yeah now that i know this is possible the answer to my question becomes obvious
though i must say i dont like that this is possible. i'd prefer identifiers to be unique
m
I agree that it's not a good idea to have a property and a function with the same name. Just like I don't like shadowing of variable names, and will recommend against it in a Code Review.
d
It's usually a bad idea, I expect that it's only allowed in Kotlin to make transition from Java easier.