我在pattern matching
学习SML
。我偶然发现了以下说法:
case
中的SML
表达式可能会出现编译错误和运行时错误。
我知道如何使用case
创建编译错误,但是如何创建运行时错误?
这可能发生在非穷举匹配的情况下:
datatype directions = North | South | East | West
fun turn d = case d of
North => East
|East => South
|South => West
这编译(虽然:Warning: match nonexhaustive
)。但是之后:
turn West
将触发运行时错误:
uncaught exception Match [nonexhaustive match failure]
我无法想到case
语句中的运行时错误,它至少不会触发警告(除了在其中一个案例中=>
之后出现的表达式本身会引发错误的琐碎方式)。