unix - 从类型中选择的结构成员,它是不可见的,不会被选中

标签 unix ocaml

我有一个使用 Unix lib 作为时间函数的函数:

let rfc822 (t: Unix.tm) : string =
    Printf.sprintf "%s, %s %s %d %s:%s:%s %s"
    (List.nth short_days t.tm_wday)
    (padInt t.tm_yday 2 "0")
    (List.nth short_month t.tm_mon)
    (t.tm_year + 1900)
    (padInt t.tm_hour 2 "0")
    (padInt t.tm_min 2 "0")
    (padInt t.tm_sec 2 "0")
    "GMT"

我收到这个警告:
ocamlbuild -libs unix,str -Is recore/src,ostd/src,owebl/src app.native
+ /usr/bin/ocamlc -c -I recore/src -I ostd/src -I owebl/src -o recore/src/time.cmo recore/src/time.ml
File "recore/src/time.ml", line 45, characters 27-34:
Warning 40: tm_wday was selected from type Unix.tm.
It is not visible in the current scope, and will not 
be selected if the type becomes unknown.
File "recore/src/time.ml", line 46, characters 14-21:
Warning 40: tm_yday was selected from type Unix.tm.
It is not visible in the current scope, and will not 
be selected if the type becomes unknown.
File "recore/src/time.ml", line 46, characters 4-28:
Error: This expression has type 'a -> string
   but an expression was expected of type string
Command exited with code 2.
Compilation unsuccessful after building 13 targets (12 cached) in 00:00:00.
Makefile:8: recipe for target 'old' failed
make: *** [old] Error 10

我该如何处理这个警告?我宁愿避免open如果可能的话,使用 Unix 模块。

(请忽略实际的编译错误。)

最佳答案

你可以写t.Unix.tm_yday

$ ocaml
        OCaml version 4.02.1

# let f (t: Unix.tm) = t.tm_yday;;
Warning 40: tm_yday was selected from type Unix.tm.
It is not visible in the current scope, and will not
be selected if the type becomes unknown.
val f : Unix.tm -> int = <fun>

# let f (t: Unix.tm) = t.Unix.tm_yday;;
val f : Unix.tm -> int = <fun>

更新

要在文档中找到它,您需要查找 field 的定义。 :
field   ::= [ module-path . ]  field-name

字段名称可以在字段名称本身之前包含模块名称(或一系列模块名称,对于嵌套模块)。

更新 2

在本地打开模块也有两种语法。对于这个微小的功能,它们看起来有点矫枉过正,但对于更复杂的功能可能会更整洁。模块的符号在整个子表达式中直接可用。
$ ocaml
        OCaml version 4.02.1

# let f t = Unix.(t.tm_yday);;
val f : Unix.tm -> int = <fun>

# let f t = let open Unix in t.tm_yday;;
val f : Unix.tm -> int = <fun>

这些在 Local opens 中被记录为语言扩展。 .

关于unix - 从类型中选择的结构成员,它是不可见的,不会被选中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30677014/

相关文章:

linux - 无法将路径用作字符串

linux - 批处理和守护进程之间的区别

linux - 哪个命令可以快速搜索一行中的连续模式

linux - SSH 使用 python 脚本

ocaml - 尝试平均变量时出现语法错误

linux - 使用 sh 命令运行时替换 myscript.sh 文件中的变量值

ocaml - 如何在 OCaml 中打印调用函数的文件位置?

c - ocaml c 互操作传递结构

haskell - 是否有在 GPU 上运行的函数式编程语言?

ocaml - 如何将 OCaml 目标文件与核心模块链接?