How to auto-generate stubs from mli file?

╄→尐↘猪︶ㄣ 提交于 2019-12-07 18:41:29

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!