https://kotlinlang.org logo
Title
b

Ben Edwards

08/13/2022, 1:40 PM
Curios, for constants you use 'const val', why not just const?
c

Chris Lee

08/13/2022, 5:54 PM
the Kotlin syntax for constants is
const val
. These are for values that are known at compile time, and are inlined into where they are used. This differs from
val
that assigns values at runtime.
s

Stephan Schroeder

08/14/2022, 9:18 AM
Ben has a point, why not just
const
🤔 It's not like you could have a
const var
, so composability isn't a thing here and the chosen solution is longer which unfortunately reminds me of Java's
final var
.
e

ephemient

08/14/2022, 7:36 PM
const
should be left as a modifier to keep future uses open
b

Ben Edwards

08/14/2022, 10:02 PM
My point is the val does not give you anything. const var would be nonsense. I'm curious why the people who created the syntax decided on it.
e

ephemient

08/14/2022, 10:03 PM
const fun
might have a future meaning
the design is not final but you can see some plans, https://youtrack.jetbrains.com/issue/KT-14652
s

Stephan Schroeder

08/21/2022, 10:48 AM
so why would you need
const x = 5
and ``const val x = 5`` in the language. My uneducated impression is that you could always reinterprete a runtime
const x = 5
(with the current
const val
semantics) to also have a comptime semantic without breaking any existing code because comptime doesn't exist yet.
c

Chris Lee

08/21/2022, 2:31 PM
presently, Kotlin constants (
const val
) are only evaluated at compile time and do not exist at runtime.
s

Stephan Schroeder

08/25/2022, 2:51 PM
so? how is that different from something that would be denoted by only
const
in other languages?
e

ephemient

08/25/2022, 11:24 PM
Chris: that's not totally true, they still exist at runtime even though all static references are inlined, at least on JVM
Stephan: not sure which languages you're comparing to Python/Swift - no
const
Java - no `const`;
final
qualifier is overloaded C/C++/Objective-C/D -
const
is qualifier, but means something different Go/JS -
const
is declaration, but means something different Rust - the only one where
const
has a meaning like
const val
in Kotlin
s

Stephan Schroeder

08/28/2022, 7:09 AM
@ephemient as far as I know,
comptime
values/functions in Zig are available at compile-time and runtime. I just got an idea, that might explain why
const val = 5
might be better idea than simply the keyword
const x = 5
.
const
is probably composable after all, not with
var
but with
fun
(possibly in the future)!?! I do prefer the idea of using
const val
and
const fun
over
const
and
const fun
.