Skip to content

结构体与枚举

当单个值不够用时,用结构体和枚举来为你的数据建模。 两者都可以像其他类型一样被导出,并从你的业务逻辑中使用。

用 struct 关键字定义具名结构体:

export struct Player {
name: string,
score: int,
}
export component Example {
in-out property<Player> player: { name: "Foo", score: 100 };
}
slint

字段可以声明默认值,对于一次性的形状,你可以直接使用匿名的 结构体类型,例如 {name: string, score: int}。

用 enum 关键字定义枚举:

export enum CardSuit { clubs, diamonds, hearts, spade }
export component Example {
in-out property<CardSuit> card: spade;
out property<bool> is-clubs: card == CardSuit.clubs;
}
slint

用 CardSuit.spade 引用某个值,或者在类型已知的地方省略枚举名—— 就像上面的绑定那样。

关于字段默认值、名字省略以及默认值,参见语言参考中的 结构体与枚举章节。


© 2026 SixtyFPS GmbH