Domanda

Is it possible to make ocamlc produce custom warnings when compiling? I have some half-finished code, and I want to be reminded that it's not finished. As an example: I want to write a reverse function. I can't be bothered to work out how to code it up at the moment, so I just write something that type-checks.

let reverse xs = xs

I would like to write something like

let reverse xs = Compiler_Warning "TODO: reverse"; xs

Is that possible? I'd be interested to hear any tricks for achieving a similar result.

È stato utile?

Soluzione

A common trick is to raise an exception with a failwith "not implemented" or just assert false.

This will give you a warning if you have unused parameters in the unimplemented function. Although, they are disable by default. So you need to add -Wall option, to enable them.

Also, you can just add an intentionally unused variable in your implementation, like:

let reverse xs =
  let unimplemented = () in 
  xs

Altri suggerimenti

You might be interested by the cppo pre-processor that describes itself as a cpp equivalent for OCaml programs and features a #warning directive. It seems to be available through opam.

You can do this with extension points, and currently the plan is to annotate deprecated functions in a similar fashion. As ivg has mentioned, failwith "TODO" is standard for what you want, but you will not get compile time warnings --though you may get unused argument warnings for each argument in the function.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top