type picture enum {
Gif
Jpg
Png
}
func create_pictures() -> array[picture] {
let ary: array[picture] = array_make(10, picture::Gif)
var i = 0
while i < 10 {
if i < 3 {
ary.set(i, picture::Gif)
} else if i >= 3 && i < 7 {
ary.set(i, picture::Jpg)
} else {
ary.set(i, picture::Png)
}
i = i + 1
}
ary
}
let pictures: array[picture] = create_pictures()
func init {
var i = 0
while i < pictures.length() {
let picture = pictures[i]
match picture {
Gif => "picture is Gif\n".output()
}
i = i + 1
}
}
====== Compilation Statistics ======
Wasm size: 851B
Time cost: 5ms
---
picture is Gif
picture is Gif
picture is Gif
RuntimeError: unreachable
---
1 个赞
目前我们的 pattern match 还没有上类型检查,实际你没有指定 Jpg 和 Png 这两种 case 下应当如何处理。默认我们会当做这两种case不会遇到,如果被执行到就会抛出 unreachable 异常。如果这不是你想要的,你可以改成这样:
match picture {
Gif => "picture is Gif\n".output()
| Jpg => ()
| Png => ()
}
2 个赞