Hey can somebody give me proper example why val ma...
# getting-started
i
Hey can somebody give me proper example why val may be not initialized at compile time (so we need to add
const
modifier) ?
a
igor.wojda: where did you read that?
i
This is the main reason why
const
exist. isn't it? e.g. annotation processor must know argument value at compile time and val is not accepted
Copy code
val answer: Int = 42
const val MAX_LOG_ENTRIES = 100

@MyLogger(MAX_LOG_ENTRIES ) // OK
class Test {}

@MyLogger(answer) // error
class Test2 {}

annotation class MyLogger(val value: Int)
a
val may not be initialized at compile time when referring a complex type
val outStream = FileOutputStream("/tmp/foo")
only String and primitives can be known at compile time
same rules as for annotations attributes in java
i
thx