Skip to content

Support "sealed" keyword on interfaces and unions in Kotlin #868

@kbrooks

Description

@kbrooks

Is your feature request related to a problem? Please describe.
Add sealed keyword` to generated interfaces and unions in order to enable exhaustive type-checking in written code. For example

sealed interface Animal {}
data class Cat(val foo: String): Animal  
data class Dog(val bar: String): Animal  

fun cases(obj: Animal) {                                
    return  when (obj) {
        is Cat -> "foo"
        is Dog -> "bar"; // Removing this line will fail compilation 
    }   
}

The Kotlin language will default to exhaustive when statements (expressions are already exhaustive) in version 1.7, which will make it easier to take advantage of this feature. https://kotlinlang.org/docs/whatsnew1530.html#exhaustive-when-statements-for-sealed-and-boolean-subjects

Describe the solution you'd like
A generateSealedInterfaces build parameter that if true will add the sealed keyword to generated interfaces in Kotlin

Describe alternatives you've considered
n/a

Metadata

Metadata

Assignees

Labels

enhancementNew feature or requestkotlinPull requests that update Kotlin code

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions