Initially I wanted to achieve
class NotationDiceRoll(notation: String) {
val rolls: Int
val sides: Int
init {
parseNotation(notation)
}
This feature is called a destructuring declaration, and that's what it is, a declaration of new variables with immediate assignment to them. It's not just that variables declared with val
can't be used in later destructuring, no variables declared earlier can. For example, the following doesn't work either:
var x = 0
var y = 0
(x, y) = Pair(1, 2)
It's worth noting though that the feature you're looking for (destructuring assignments) was one of the possible future features for Kotlin out of the 20 cards that were available to be voted on at the Kotlin 1.1 event. While the online survey is no longer up, you can see the description of the feature on this image, it's card number 15. It's a bit hard to make out, so here's what's on it:
Kotlin already has destructuring declarations:
val (name, address) = findPerson(...)
Some users request destructuring assignments,
ie. assign to multiple previously declared var
's:
var name = ...
...
var address = ...
...
(name, address) = findPerson(...)
Do you need this feature?
Update: here's an official doc with the proposed features, and here's the survey results.