问题
Suppose I have some interface file mylib.mli like
var foo : 'a list -> int
val bar : f:('a -> 'b) -> 'a list -> 'b list
val baz : f:('a -> bool) -> 'a list -> 'a list
val frobnitz : init:'acc -> f:('acc -> 'a -> 'acc) -> 'a list -> 'acc
val frobozz : 'a list -> 'a list -> 'a list
val quux : 'a list list -> 'a list
Is there an automated way to generate the corresponding mylib.ml as a collection of stubs? (By "stub" I mean a "minimal, interface-implementing function".)
回答1:
AFAIK, there is no such tool currently written. Maybe, because it is not a big work to do it manually. The easiest way to write a stub is:
let foo = failwith "not implemented"
or you can just make it in a following way
let stub _ = failwith "unimplemented"
let foo = stub
let bar = stub
...
回答2:
For what it's worth, the -i flag of the compilers translates in the other direction.
$ cat stubs.ml
let foo (x: 'a list) = 3
let bar ~f: f l = List.map f l
let baz ~f: p l = List.filter p l
$ ocamlc -i stubs.ml
val foo : 'a list -> int
val bar : f:('a -> 'b) -> 'a list -> 'b list
val baz : f:('a -> bool) -> 'a list -> 'a list
Update
Here's some interesting discussion in the context of Haskell:
Given a Haskell type signature, is it possible to generate the code automatically?
来源:https://stackoverflow.com/questions/26821053/how-to-auto-generate-stubs-from-mli-file