Comments – how do I discard bindings in Ocaml?

I want to comment the function in the external library as deprecated to ensure that it will not be used in my project We assume that the library provides the following modules:

module Lib : sig
  val safe_function : int -> unit
  val unsafe_function : int -> int -> unit
end = struct
  let safe_function _ = ()
  let unsafe_function _ _ = ()
end

My project has a util Ml file, I open it in each file Among them, I want to do something similar:

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]

Compile the following usage Ml file

open Util

let _ = unsafe_function 0 0
let _ = foo 0

let _ = A
let f (x : t) = x

The following warnings are generated:

$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

Therefore, properties not recommended on let bindings will not be triggered, but properties on type definitions and constructors will be triggered Attribute syntax seems to allow both

I found this answer, but it seems outdated because:

>It explicitly says that it "only applies to values (not types)", which is not true (no longer?), As shown in the above example. > The document clearly states that the comment "can be applied to most items in a signature or structure."

Solution

I'm not sure what the specific syntax is (your suggestion sounds correct and corresponds to the parser code, so it may be an error in the compiler), but you can (AB) use the module system to do this:

include (Lib : sig
    val unsafe_function : int -> int -> unit
    [@@ocaml.deprecated "Use safe_function instead."]
  end)

let _ = unsafe_function 0 0 (* warning here *)
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>