Initialization of val by destructuring in Kotlin

前端 未结 1 676
别那么骄傲
别那么骄傲 2021-01-21 12:19

Initially I wanted to achieve

class NotationDiceRoll(notation: String) {
    val rolls: Int
    val sides: Int

    init {
        parseNotation(notation)
    }
         


        
1条回答
  •  走了就别回头了
    2021-01-21 13:08

    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:


    15 Destructuring assignments

    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.

    0 讨论(0)
提交回复
热议问题