警告40的含义是什么:此记录...包含在当前作用域中不可见的字段 [英] What is the meaning of Warning 40: this record ... contains fields that are not visible in the current scope

查看:100
本文介绍了警告40的含义是什么:此记录...包含在当前作用域中不可见的字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑以下代码:

module A =
  struct
    type r = { i : int; s: string }
  end

module B =
  struct
    type r = { i : int; s : string }
  end


let f (x : A.r) : B.r = match x with
    { i; s } -> { i = 2*i; s = "" }

两个模块定义完全相同的记录.函数f将A记录转换为B记录.该警告已在编译期间发出,但也可以交互显示.在ocaml cli上,似乎对f的调用完成了预期的操作:

Two modules define exactly the same record. A function f converts an A record to a B record. The warning is already emitted during compilation, but also visible interactively. On the ocaml cli, it seems that a call to f does the intended thing:

# let x = f { i = 5; s = "ab" };;
Characters 10-29:
  let x = f { i = 5; s = "ab" };;
            ^^^^^^^^^^^^^^^^^^^
Warning 40: this record of type Shadow.A.r contains fields that are 
not visible in the current scope: i s.
They will not be selected if the type becomes unknown.
val x : Shadow.B.r = {Shadow.B.i = 10; s = ""}

我在 lexifi.com 解释了该问题以及一些常见的

I found a blog posting on lexifi.com which explains the problem and some common solutions. What i don't understand is the actual error message:

  • 类型变为未知是什么意思?
  • 未选择字段是什么意思?
  • 并从以上两个结果中得出:忽略警告需要满足哪些条件?

推荐答案

在这种情况下,记录类型是已知的,因为您提供了注释. 如果删除了注释,则类型将变为未知,并且代码的含义可能会更改.

The type of the record is known in this case because you provided annotations. If you removed the annotations, the type would become unknown and the meaning of the code might change.

OCaml的理念是添加和删除类型注释不应影响程序的含义,因此会产生警告.

The philosophy of OCaml is that adding and removing type annotations should not affect the meaning of a program, so a warning is produced.

您可以通过将相关字段纳入范围来避免此警告.对模块A中定义的字段执行此操作需要打开A以使其内容进入范围,或使用模块限定字段名称.例如:

You can avoid this warning by bringing the relevant field into scope. Doing this for a field that is defined within a module A entails either opening A to bring its contents into scope, or qualifying the name of field with the module. For example:

module A = struct ... end

let test r = r.A.field

let test2 r = let open A in r.field

open A 

let test3 r = r.field

这篇关于警告40的含义是什么:此记录...包含在当前作用域中不可见的字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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