避免出现警告“警告21:此语句从不返回(或具有不正确的类型.)". [英] Avoid the warning "Warning 21: this statement never returns (or has an unsound type.)"

查看:106
本文介绍了避免出现警告“警告21:此语句从不返回(或具有不正确的类型.)".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我第一次遇到以下警告警告21:此语句永远不会返回(或类型不正确."),而且我不知道如何解决它.

This is the first time that I met the following warning "Warning 21: this statement never returns (or has an unsound type.)" and I don't have an idea how to fix it.

let display_committers_stats response = match response##readyState with
  | XmlHttpRequest.DONE ->
    begin match response##status with
      | 200 ->
        Js_client_ui.create_menu_tabs "GitSearchTabs";
        let l =
          Json_parser.get_commits (Js.to_string response##responseText) in
        let values = 
          Json_parser.group_commits_by_user l
          |> List.map (fun (author, commits) ->
              Js_data.create_discreteBar_item author (float_of_int commits))
          |> Array.of_list 
          |> Js.array in
        let discreteBar_chart =
          Js_data.create_discreteBar_chart "Commits-impact" values in
        let js_arr = Js.array ([|discreteBar_chart |]) in 
        Js.Unsafe.fun_call
          (Js.Unsafe.variable "create_discreteBar_chart")
          [|
            Js.Unsafe.inject ((js_arr))              
          |];
        let js_arr =
        l
        |> List.mapi (fun i commit ->
            Js_data.create_timeline_data i commit.Git_data.message
              commit.Git_data.time)
        |> Array.of_list
        |> Js.array in
        Js.Unsafe.fun_call
        (Js.Unsafe.variable "create_timeline")
        [|
          Js.Unsafe.inject ((js_arr))              
        |]
      | _ -> Printf.printf "Unexcepted status\n" end
  | _ -> Printf.printf "Unexcepted state\n"

警告显示以下行:

Js.Unsafe.fun_call
          (Js.Unsafe.variable "create_discreteBar_chart")
          [|
            Js.Unsafe.inject ((js_arr))              
          |];

要在Ocaml中执行倍数表达式,我知道问题在于要使用;在表达式之间,但是现在我的函数出了什么问题?我可以给我一些提示吗?

For execute multiples expressions in Ocaml, I know that the issue is to use ; between the expressions but what's is wrong in my function now ? Can I have some tips ?

推荐答案

尝试将通话包装在ignore中,而不是Js.Unsafe.fun_call ...;ignore (Js.Unsafe.fun_call ...);中.

Try wrapping the call in ignore, i.e. instead of Js.Unsafe.fun_call ...;, ignore (Js.Unsafe.fun_call ...);.

发生这种情况的原因是因为您的JS函数调用的结果类型为"'b",该结果类型不依赖于任何参数类型.在OCaml类型系统中,这通常意味着函数不会返回,因为从零开始产生"任意类型'b值的唯一方法是引发异常-即,放弃尝试生产它.

The reason this is happening is because your JS function call has a result type "'b", which is not dependent on any of the argument types. In the OCaml type system, this typically means that the function doesn't return, because the only way to "produce" a value of an arbitrary type 'b from nothing is to raise an exception – that is, to give up trying to produce it.

序列表达式(分号)e1; e2表示先完成第一个表达式,然后再完成第二个表达式.在这种情况下,OCaml担心您的e1(JS函数调用)由于其不受约束的结果类型而无法完成,如上所述.这会使序列表达式变得毫无意义,这就是为什么您会收到警告.但是,我们知道e1具有不受限制的结果类型的原因不是因为它没有完成,而是因为我们对JS使用了不安全的绑定.

The sequence expression (semicolon) e1; e2 means complete the first expression, then complete the second one. In this case, OCaml is worried that your e1 (the JS function call) won't complete because of its unconstrained result type, as explained above. That would make the sequence expression pointless, which is why you get the warning. However, we know that the reason e1 has an unconstrained result type isn't because it doesn't complete, but because we are using an unsafe binding to JS.

要解决此问题,请将e1包装在对ignore的调用中,该函数可以接受任何内容并求和为单位.现在,;将在其左侧看到" unit,而不是不受约束的'b.

To get around this, wrap e1 in a call to ignore, which is a function that takes anything and evaluates to unit. Now, ; will "see" unit on its left instead of an unconstrained 'b.

通常来说,您总是希望在;的左侧有一个类型为unit的表达式.因此,即使您有一个表达式的计算结果为受约束的类型(例如具体类型int或参数类型中提到的类型参数),如果该类型不是unit,则仍应将其包装起来ignore中的表达式.

Generally speaking, you always want to have an expression of type unit on the left of a ;. So, even if you have an expression that evaluates to a constrained type (such as a concrete type int or a type parameter that is mentioned in the argument types), if that type is not unit, you should still wrap that expression in ignore.

这篇关于避免出现警告“警告21:此语句从不返回(或具有不正确的类型.)".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆