好吧,现在Kotlin 正式出局了,我又开始玩它了,我很困惑,我需要在sealeddata的优势之间做出 Select ,但不知何故,这两者都不能兼得.

This, for example, seems to make sense to me, but does not compile:

sealed class Expr {
    data class Const(val number: Double) : Expr()
    data class Sum(val expr1 : Expr, val expr2 : Expr) : Expr()
}

因为数据类不能扩展其他类.

Is there something I am missing?

推荐答案

Shortly before having entered Beta state, Kotlin team had decided to add certain limitations on data classes usage (see this post) because of the problems they caused in class hierarchies.

One of the limitations is that data class should not subtype another class, only interfaces are allowed. Consequently, data classes cannot derive from a sealed class.

This was a necessary measure to avoid further postponing the 1.0 release. Some of the limitations were said to be lifted in future releases, once the problematic cases are thoroughly reviewed and a good design solution is found.

Kotlin相关问答推荐

如何在Jetpack Compose中的列中渲染图像

如何在Kotlin中为两个数据类创建可重用的方法?

KTOR';S函数`staticResources`在Kotlin本机目标上不可用

如何更改默认推断没有接收者的函数类型?

Kotlin 说不需要强制转换,但删除后会出现新警告

按钮无法在 Android Studio 上打开新活动

如何从 var list 或可变列表中获取列表流

正则表达式 FindAll 不打印结果 Kotlin

奇怪的 cotlin check Not Null 参数错误

你怎么知道什么时候需要 yield()?

如何从kotlin中的ArrayList中删除所有元素

Kotlin 中多个 init 块的用例?

无法删除部分子项.这可能是因为进程打开了文件或将其工作目录设置在目标目录中

为什么我在使用 Jetpack Compose clickable-Modifier 时收到后端内部错误:Exception during IR lowering error?

将 jetpack compose 添加到现有元素

Kotlin:如何在活页夹中返回正在运行的服务实例?

如何在协程之外获取 Flow 的值?

(kotlin的Moshi)@Json vs@field:Json

保存对象时未填充 Spring Boot JPA@CreatedDate @LastModifiedDate

将 Double 转换为 ByteArray 或 Array Kotlin