SML的case表达式中的运行时错误

问题描述 投票:1回答:1

我在pattern matching学习SML。我偶然发现了以下说法:

case中的SML表达式可能会出现编译错误和运行时错误。

我知道如何使用case创建编译错误,但是如何创建运行时错误?

sml
1个回答
0
投票

这可能发生在非穷举匹配的情况下:

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语句中的运行时错误,它至少不会触发警告(除了在其中一个案例中=>之后出现的表达式本身会引发错误的琐碎方式)。

© www.soinside.com 2019 - 2024. All rights reserved.