OCaml属性 [英] OCaml attributes

查看:70
本文介绍了OCaml属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在查看手册时发现,OCaml中有一些属性用于声明已弃用的内容(请参见

I was looking at the manual and found that there are attributes in OCaml for declaring things as deprecated (see http://caml.inria.fr/pub/docs/manual-ocaml/extn.html), but I can not figure out how to get them to be recognized by the compiler.

这是我编写的程序:

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 ...],但结果相同).我在跑步时没有收到任何警告:

(I also tried [@@deprecated ...] rather than [@@ocaml.deprecated ...] with the same results). I don't get any warnings when I run:

ocamlbuild src/trial.byte

我的_tags文件中是否需要设置某些内容?这里还有我想念的东西吗?

Is there something that I need to set up in my _tags file? Is there something else that I'm missing here?

推荐答案

不建议使用的批注仅适用于值(不适用于类型),并且大多数用于签名.在您的情况下,请按以下步骤操作:

The deprecated annotation is only available for values (not on types), and mostly in signatures. In your case, here how it should be done:

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属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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