Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Exception 为什么可以';我不能给只引发异常的函数的参数加上标签吗?_Exception_Ocaml_Argument Passing - Fatal编程技术网

Exception 为什么可以';我不能给只引发异常的函数的参数加上标签吗?

Exception 为什么可以';我不能给只引发异常的函数的参数加上标签吗?,exception,ocaml,argument-passing,Exception,Ocaml,Argument Passing,我试图编写一个只引发异常的函数。它可以工作,但是当参数被标记时,我不能在另一个函数中使用它 下面是一个简单的例子: let raise_error ~m = failwith m let test = function | true -> raise_error "ok" | false -> raise_error "ko" 有一个警告,该函数没有我期望的类型: Warning 20: this argument will not be used by the func

我试图编写一个只引发异常的函数。它可以工作,但是当参数被标记时,我不能在另一个函数中使用它

下面是一个简单的例子:

let raise_error ~m = failwith m

let test = function
  | true -> raise_error "ok"
  | false -> raise_error "ko"
有一个警告,该函数没有我期望的类型:

Warning 20: this argument will not be used by the function.
Warning 20: this argument will not be used by the function.
val test : bool -> m:string -> 'a = <fun>
我谨此致辞:

Warning 20: this argument will not be used by the function.
Error: This expression has type m:string -> 'a
but an expression was expected of type string
我不明白是什么错了,因为如果论点没有被标注,它就会起作用


有没有办法解决这个问题?

您可以在调用站点标记参数:

let raise_error ~m = failwith m

let test = function
  | true -> raise_error ~m:"ok"
  | false -> raise_error ~m:"ko"

let test2 = function
  | true -> "ok"
  | false -> raise_error ~m:"ko"
更好的解决方案是重写
raise\u error
以不使用带标签的参数


问题是编写的
raise\u error
的类型为
m:string->'a
,因此您可以将任意数量的未标记参数传递给它,结果表达式仍然是
m:string->'a
类型的函数。这是一个容易出错且令人惊讶的问题,因此您应该避免使用带有发散函数的标记参数。

您可以在调用站点标记参数:

let raise_error ~m = failwith m

let test = function
  | true -> raise_error ~m:"ok"
  | false -> raise_error ~m:"ko"

let test2 = function
  | true -> "ok"
  | false -> raise_error ~m:"ko"
更好的解决方案是重写
raise\u error
以不使用带标签的参数


问题是编写的
raise\u error
的类型为
m:string->'a
,因此您可以将任意数量的未标记参数传递给它,结果表达式仍然是
m:string->'a
类型的函数。这是一个容易出错且令人惊讶的问题,因此您应该避免使用带有发散函数的标记参数。

您可以在调用站点标记参数:

let raise_error ~m = failwith m

let test = function
  | true -> raise_error ~m:"ok"
  | false -> raise_error ~m:"ko"

let test2 = function
  | true -> "ok"
  | false -> raise_error ~m:"ko"
更好的解决方案是重写
raise\u error
以不使用带标签的参数


问题是编写的
raise\u error
的类型为
m:string->'a
,因此您可以将任意数量的未标记参数传递给它,结果表达式仍然是
m:string->'a
类型的函数。这是一个容易出错且令人惊讶的问题,因此您应该避免使用带有发散函数的标记参数。

您可以在调用站点标记参数:

let raise_error ~m = failwith m

let test = function
  | true -> raise_error ~m:"ok"
  | false -> raise_error ~m:"ko"

let test2 = function
  | true -> "ok"
  | false -> raise_error ~m:"ko"
更好的解决方案是重写
raise\u error
以不使用带标签的参数

问题是编写的
raise\u error
的类型为
m:string->'a
,因此您可以将任意数量的未标记参数传递给它,结果表达式仍然是
m:string->'a
类型的函数。这很容易出错,而且令人惊讶,因此您应该避免使用带有发散函数的标记参数