无法解构Functor(模块)

时间:2018-03-12 23:55:55

标签: ocaml reason

这种语法非常有用 - 这是否有理由不起作用?谢谢!

module Foo = {
  let bar: string = "bar"
};

let bar = Foo.bar; /* works */
let { bar } = Foo; /* Unbound record field bar */

Try it online!

2 个答案:

答案 0 :(得分:7)

你能做的最接近的是:

module Foo = {
  let bar = "bar";
  let baz = "baz";
};

let (bar, baz) = Foo.(bar, baz);

答案 1 :(得分:3)

无法在OCaml / Reason中对模块进行解构。

等效的OCaml代码在第3行产生语法错误

module Foo = struct let bar: string = "bar" end
let bar = Foo.bar
let struct bar end = Foo

File "", line 3, characters 4-10: Error: Syntax error

如果您可以将Foo中指定的所有值放入本地范围,那么您可以使用open Foohttps://reasonml.github.io/try.html?reason=LYewJgrgNgpgBAMRCOBeOBvOsAucBGAhgE4BccAzjsQJYB2A5mnAERHEtwC+A3AFAgADjDqJk-AFIUAdFBAMAFOwCUQA

module Foo = { let bar: string = "bar" };
open Foo;
Js.log(bar)

如果您还想通过当前模块导出导出Foo中的所有定义,您可以include Foohttps://reasonml.github.io/try.html?reason=LYewJgrgNgpgBAMRCOBeOBvOsAucBGAhgE4BccAzjsQJYB2A5mnAERHEtwC+A3AFD0AxlAhh4SEPwBSFAHRQQDABTsAlEA

module Foo = { let bar: string = "bar" };
include Foo;
Js.log(bar)