0 ocaml exception pattern-matching
我有这个功能
let f = function
| 1 -> "a"
| 2 -> "b"
| _ -> failwith "Argument should be less than 3 and more than 0 but it was found to be x"
Run Code Online (Sandbox Code Playgroud)
如何将此处的值设置x为等于函数的输入?
小智 5
sprintf您可以使用Printf 模块中存在的标准库函数。
| x -> failwith (Printf.sprintf "Argument should be ... but it was %d" x)
Run Code Online (Sandbox Code Playgroud)
不过,我建议您使用invalid_arg而不是failwith因为您由于无效参数而引发异常。
查看OCaml 文档的此页面。