OCaml 3.12中的头等舱模块:它们会使哪种事情变得更容易(或可能)? [英] First class modules in OCaml 3.12: What kinds of things will they make easier (or possible)?

查看:64
本文介绍了OCaml 3.12中的头等舱模块:它们会使哪种事情变得更容易(或可能)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我听说OCaml 3.12中有一流的模块".他们将提供什么优势?什么样的孩子会更容易?他们想解决什么问题?一个简单的例子就足够了.

I've heard that "first class modules" are coming in OCaml 3.12. What advantages will they offer? What kids of things will be easier? What problem are they trying to solve? A simple example would suffice.

推荐答案

这只是一个可能的应用程序,但是一流的模块使对存在类型的编码变得容易,基本上,一个模块使用此类型包装一个存在性类型和一个值) .例如,请参阅Alain Frisch关于动态类型的工作(摘自Alain Frisch关于dyntypes的代码:

It's only one possible applications, but first class modules make it easy to encode existential types, with basically a module packing an existential type and a value using this type). For example, See Alain Frisch work on Dynamic types (code taken from Alain Frisch work on dyntypes : http://caml.inria.fr/cgi-bin/viewvc.cgi/ocaml/branches/dyntypes/stdlib/dyntypes.ml?view=markup )

module type DYN = sig
  type t
  val x: t
  val t: t ttype
end

type dyn = (module DYN)

let dyn (type s) t x =
  let module M = struct
    type t = s
    let x = x
    let t = t
  end
  in
  (module M : DYN)

这里的想法是,"ttype"是该类型的具体表示形式,是具有Int,Float构造函数等的代数数据类型,您在此处拥有一个值,该值的类型被隐藏,但带有以下形式的具体表示形式:这种类型,您可以使用它来获得更安全的序列化/反序列化.

The idea here is that "ttype" is a concrete representation of that type, an algebraic datatype with Int, Float constructors and so on, and you have here a value, whose type is concealed, but that carries a concrete representation of that type, that you can use for example to get a safer serialization/deserialization.

这篇关于OCaml 3.12中的头等舱模块:它们会使哪种事情变得更容易(或可能)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆