我正在尝试实现类似以下C的类似数据类定义:
struct A {
int b;
struct {
int d;
} c;
};
Run Code Online (Sandbox Code Playgroud)
According to Dmitry Jemerov it is possible, but he didn't provide any code sample. https://discuss.kotlinlang.org/t/is-there-a-reason-for-not-allowing-inner-data-classes/2526/5
You can simply make it nested inside another class. Nested classes can be data classes.
How it should be done if it is true?
No, Kotlin does not support anonymous structures like that.
You can both literally nest the classes:
data class A(
val b: Int,
val c: C
) {
data class C(
val d: Int
)
}
Run Code Online (Sandbox Code Playgroud)
Or use a more common syntax:
data class C(
val d: Int
)
data class A(
val b: Int,
val c: C
)
Run Code Online (Sandbox Code Playgroud)
Actually, there is no need in "nesting" here. The difference will be mostly in the way you access the C class: A.C or just C.
| 归档时间: |
|
| 查看次数: |
50 次 |
| 最近记录: |