是“动态的” OCaml 中可以进行模式匹配吗?
那么在 Ocaml 中你可以在编译时做到这一点:
let handle item = match item with
| 1 -> "Do this";
| 2 -> "Do that";
| n -> "Do Nothing";
;;
有没有办法在运行时实现它?就像某种责任链模式?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,有这样的模式。
假设您正在处理一个必须接受整数并返回字符串的函数,并且默认情况下它将为每个整数返回
“Do Nothing”
:如果您想说
,你可以这样做:
Yes, there is such a pattern.
Let's assume that you're dealing with a function that must accept an integer and return a string, and that by default it will return
"Do nothing"
for every integer :If you wish to say that
"Do this"
should be returned when the argument is 1, you can do: