can someone give me a simple explanation why they ...
# getting-started
f
can someone give me a simple explanation why they didn't just use
const
as a standalone keyword instead of
const val
? There is no other
const
combination as far as I see it
1
k
`const`s are assigned in compile time, `val`s are assigned in runtime.
Which is why you can only assign primitives and strings to a
const
, and can’t assign function results.
But I guess your question is more about why not just
const
?
f
yep, why not just const
k
const
is a modifier in this case, where as
val
is a keyword.
val
indicates a value that cannot be reassigned and by default it’s assigned in runtime.
In comparison
const
is a modifier to a
val
keyword, it says how the value is being assigned.
It’s very similar to visibility modifiers
you have
class
keyword and
private
modifier
having some sort of
privateClass
keyword (since we have functions and other things) would be redundant, if it makes sense
Having
const
is more flexible this way
And less overlapping
f
ok, but why didn't they make it a keyword when it's never used in another context?
k
Flexibility
I saw some discussions around being able to apply
const
modifier on functions
f
so they want to keep the possibility open?
Right now
const
can't be used somewhere else, or can it?
k
That’s my understanding
Yeah, I don’t think it can be used elsewhere currently
f
ok, thank you very much
i
const
is a soft keyword, that means it can be used in other places just as a usual identifier, e.g.
Copy code
val const = kotlin.random.Random.nextInt()
println(const)
k
neat
f
but that's not the reasoning behind that is it?
It's just a side-effect
a
AFAIK, having as few hard keywords as possible is one of the goals