https://kotlinlang.org logo
#codereview
Title
# codereview
l

LeoColman

11/09/2023, 9:56 PM
Copy code
sealed class IssueStatus(
    val transitionsTo: List<IssueStatus>
) {
    constructor(vararg transitionsTo: IssueStatus) : this(transitionsTo.toList())

    data object Closed : IssueStatus()
    data object Acknowledged : IssueStatus(New, Closed)
    data object New : IssueStatus(Acknowledged, Closed)
}
How would you solve this issue? References inside
transitionsTo
are null, as they're referencing before it's been declared
e

ephemient

11/09/2023, 10:03 PM
Copy code
sealed class IssueStatus {
    abstract val transitionsTo: List<IssueStatus>

    data object Closed : IssueStatus() {
        override val transitionsTo: List<IssueStatus>
            get() = emptyList()
    }

    data object Acknowledged : IssueStatus() {
        override val transitionsTo: List<IssueStatus>
            get() = listOf(New, Closed)
    }

    data object New : IssueStatus() {
        override val transitionsTo: List<IssueStatus>
            get() = listOf(Acknowledged, Closed)
    }
}
sure would be nice if https://youtrack.jetbrains.com/issue/KT-8970 prevented you from getting into this state though
1
k

Klitos Kyriacou

11/10/2023, 9:56 AM
By the way, when converting a
vararg
parameter, you might as well use
asList()
instead of
toList()
because the array instance is one the compiler has auto-generated, and not one that you can change yourself, therefore there's no need to guard against changes by copying the array.
👍 1
e

ephemient

11/10/2023, 9:57 AM
(there is still a need to guard against changes if it could potentially be called by Java, although that's not the case here)
2