我想要将外部库中的函数注释为已弃用,以确保它不会在我的项目中使用.我们假设该库提供以下模块:
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,但似乎已经过时,因为:
>它明确地说它“仅适用于值(不适用于类型)”,这不是真的(不再是?),如上例所示.
>文档明确说明注释“可以应用于签名或结构中的大多数项目.”
解决方法
我不确定具体的语法是什么(你的建议听起来正确并且对应于解析器代码,所以它可能是编译器中的一个错误),但你可以(ab)使用模块系统来做到这一点:
include (Lib : sig val unsafe_function : int -> int -> unit [@@ocaml.deprecated "Use safe_function instead."] end) let _ = unsafe_function 0 0 (* warning here *)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。