在 OCaml 签名中放置类型声明的正确方法是什么

问题描述 投票:0回答:1

我编写了以下两个文件,虽然它可以工作,但代码会重复这些长类型声明,这感觉很奇怪。另一方面,如果我删除任一模块类型声明,则代码将不再编译。

这种东西有更合适的写法吗?或者在 .mli 和 .ml 文件中重复这些类型声明是否正确?

设置.mli

module type Element = sig
  type t
  val create : 'a -> t
  val compare : t -> t -> int
  val to_string : t -> string
end

module type Set = sig
  type t
  val empty : unit -> t
end

module Make : functor (M : Element) -> Set with type t = M.t list

设置.ml

module type Element = sig
  type t
  val create : 'a -> t
  val compare : t -> t -> int
  val to_string : t -> string
end

module type Set = sig
  type t
  val empty : unit -> t
end

module Make (M:Element) = struct
  type t = M.t list
  let emtpy () = []
end
interface ocaml signature functor
1个回答
0
投票

对此的一个很好的参考是回顾标准库是如何实现的。怀疑你能否找到比他们“更好”的做事方式。

我考虑 set.mliset.ml 你会发现它们的指定与您的代码非常相似。

© www.soinside.com 2019 - 2024. All rights reserved.