gpt4 book ai didi

OCaml 属性

转载 作者:行者123 更新时间:2023-12-01 00:41:54 26 4
gpt4 key购买 nike

我正在查看手册,发现 OCaml 中有一些属性用于声明已弃用的内容(请参阅 http://caml.inria.fr/pub/docs/manual-ocaml/extn.html ),但我不知道如何让编译器识别它们。

这是我写的程序:

let x = 1 [@@ocaml.deprecated "don't use this"]

type t = X | Y [@@ocaml.deprecated "don't use this"]

let _ =
let y = Y in
match y with
| X ->
print_string (string_of_int x)
| Y -> assert false

(我也尝试过 [@@deprecated ...] 而不是 [@@ocaml.deprecated ...],结果相同)。我运行时没有收到任何警告:
ocamlbuild src/trial.byte

有什么我需要在我的 _tags 中设置的吗?文件?还有什么我在这里想念的吗?

最佳答案

不推荐使用的注释仅适用于值(不适用于类型),并且主要用于签名。在你的情况下,这里应该如何做:

module M : sig
val x : int [@@deprecated "don't use this"]
type t =
| X [@deprecated "don't use this"]
| Y [@deprecated "don't use this"]
end = struct
let x = 1
type t = X | Y
end
open M

let _ =
let y = Y in
match y with
| X ->
print_string (string_of_int x)
| Y -> assert false

关于OCaml 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37035225/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com