简体   繁体   中英

OCaml attributes

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.

Here's the program that I wrote:

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

(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

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

Seems to work from 4.02.3, for this version, #require "ppx_jane";; before your code. With 4.03.0, it works natively.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM