OCaml explicit type signatures

回眸只為那壹抹淺笑 提交于 2019-11-28 09:37:34

OCaml has two ways of specifying types, they can be done inline:

let intEq (x : int) (y : int) : bool = ...

or they can be placed in an interface file, as you have done:

val intEq : int -> int -> bool

I believe the latter is preferred, since it more cleanly separates the specification (type) from the implementation (code).


References: OCaml for Haskellers

Im general, the syntax to let-bind a value with a constrained type is:

let val : constraint = e ...

Applied to a function, you can specify the signature as follows:

let add : int -> int -> int = fun x y -> ...

This is analogous to the syntax required to constrain a module to a signature:

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