main 函数
main 是 Dujie 的入口函数,返回类型必须是裸 widget。
当前规则
main必须是模块顶层函数- 一个模块内最多只能有一个
main main不需要exportmain不能与其他顶层符号重名main的返回类型必须显式写成widgetmain不能是泛型函数
参数写法
func main() -> widget {
text("Hello, World!")
}
main 的参数按普通 Dujie 函数规则书写,不再限制成固定几种入口形态。
例如:
func main(title: string, count: opt<int>) -> widget {
text(`${title}:${count}`)
}
func main({ title: string, count: int = 0 }) -> widget {
text(`${title}:${count}`)
}
动态入口示例
如果用户希望自己接动态数据,也可以直接写:
func main(props: map<string, any>) -> widget {
let title = props["title"];
if title is string(s) {
text(s)
} else {
panic("title must be string")
}
}
当 main 使用 map<string, any> 时:
props["k"]的结果是any- key 不存在时直接
panic - 取出值后使用
is做显式收窄
Rust 侧入口适配
语言层允许用户自由书写 main 参数,但编译后的 Rust 侧入口需要与之匹配:
- 位置参数按顺序暴露
- 具名参数可以映射为 Rust 结构体
opt<T>映射为Option<T>- 默认值由适配层补齐
哪个模块被当作应用入口,由构建或运行系统决定,不由 export 决定。