注释 – 如何在OCaml中注释让绑定弃用?

前端之家收集整理的这篇文章主要介绍了注释 – 如何在OCaml中注释让绑定弃用?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想要将外部库中的函数注释为已弃用,以确保它不会在我的项目中使用.我们假设该库提供以下模块:
  1. module Lib : sig
  2. val safe_function : int -> unit
  3. val unsafe_function : int -> int -> unit
  4. end = struct
  5. let safe_function _ = ()
  6. let unsafe_function _ _ = ()
  7. end

我的项目中有一个Util.ml文件,我在每个文件中打开.在其中,我想做类似的事情:

  1. open Lib
  2.  
  3. let unsafe_function = Lib.unsafe_function
  4. [@@deprecated "Use safe_function instead."]
  5.  
  6. let foo = (fun x -> x)
  7. [@@deprecated "BBB"]
  8.  
  9. type t =
  10. | A [@deprecated]
  11. | B [@deprecated]
  12. [@@deprecated]

编译以下usage.ml文件

  1. open Util
  2.  
  3. let _ = unsafe_function 0 0
  4. let _ = foo 0
  5.  
  6. let _ = A
  7. let f (x : t) = x

产生以下警告:

  1. $ocamlc -c -w +3 usage.ml
  2. File "usage.ml",line 6,characters 8-9:
  3. Warning 3: deprecated: A
  4. File "usage.ml",line 7,characters 11-12:
  5. Warning 3: deprecated: Util.t

因此,let-bindings上不推荐使用的属性不会触发,但类型定义和构造函数上的属性会触发. attribute syntax似乎允许两者.

我找到了this answer,但似乎已经过时,因为:

>它明确地说它“仅适用于值(不适用于类型)”,这不是真的(不再是?),如上例所示.
>文档明确说明注释“可以应用于签名或结构中的大多数项目.”

解决方法

我不确定具体的语法是什么(你的建议听起来正确并且对应于解析器代码,所以它可能是编译器中的一个错误),但你可以(ab)使用模块系统来做到这一点:
  1. include (Lib : sig
  2. val unsafe_function : int -> int -> unit
  3. [@@ocaml.deprecated "Use safe_function instead."]
  4. end)
  5.  
  6. let _ = unsafe_function 0 0 (* warning here *)

猜你在找的Java相关文章