我想要将外部库中的函数注释为已弃用,以确保它不会在我的项目中使用.我们假设该库提供以下模块:
- module Lib : sig
- val safe_function : int -> unit
- val unsafe_function : int -> int -> unit
- end = struct
- let safe_function _ = ()
- let unsafe_function _ _ = ()
- end
我的项目中有一个Util.ml文件,我在每个文件中打开.在其中,我想做类似的事情:
- open Lib
- let unsafe_function = Lib.unsafe_function
- [@@deprecated "Use safe_function instead."]
- let foo = (fun x -> x)
- [@@deprecated "BBB"]
- type t =
- | A [@deprecated]
- | B [@deprecated]
- [@@deprecated]
编译以下usage.ml文件
- open Util
- let _ = unsafe_function 0 0
- let _ = foo 0
- let _ = A
- let f (x : t) = x
产生以下警告:
- $ocamlc -c -w +3 usage.ml
- File "usage.ml",line 6,characters 8-9:
- Warning 3: deprecated: A
- File "usage.ml",line 7,characters 11-12:
- Warning 3: deprecated: Util.t
因此,let-bindings上不推荐使用的属性不会触发,但类型定义和构造函数上的属性会触发. attribute syntax似乎允许两者.
我找到了this answer,但似乎已经过时,因为:
>它明确地说它“仅适用于值(不适用于类型)”,这不是真的(不再是?),如上例所示.
>文档明确说明注释“可以应用于签名或结构中的大多数项目.”