3

在 Standard-ML 中编译多个文件是如何工作的?我有 2 个文件。

file1.sml

(* file1.sml *)
datatype fruit = Orange | Apple | None

并且file2.sml

(* file2.sml *)
datatype composite = Null | Some of fruit

如您所见file2.sml,使用file1.sml. 我怎样才能使这个东西编译?

我正在使用mosmlc.exe和编译时mosmlc file2.sml(至于这个问题):

(* file2.sml *)
use "file1.sml";
datatype composite = Null | Some of fruit

我得到:

! use "file1.sml";
! ^^^
! Syntax error.

那么,如何处理多个文件呢?

4

1 回答 1

5

您可以在Moscow ML Owner's Manual中阅读更多内容,但在您的特定情况下,以下命令应该可以工作,甚至不必use在源代码中使用:

mosmlc -toplevel file1.sml file2.sml

使用结构模式

当您想将代码组织成结构时,可以-structure使用mosmlc. 例如,给定以下文件:

你好.sml

structure Hello =
  struct
    val hello = "Hello"
  end

世界.sml

structure World =
  struct
    structure H = Hello

    val world = H.hello ^ ", World!"
  end

主.sml

fun main () =
  print (World.world ^ "\n")

val _ = main ()

您现在可以获得一个名为 的可执行文件main,如下所示:

mosmlc -structure Hello.sml World.sml -toplevel main.sml -o main

然后运行它:

$ ./main
Hello, World!

结构模式要求您的文件名和包含的结构一致,就像在 Java 中的类和文件必须具有相同的名称一样。您还可以使用.sig包含签名的文件。

于 2016-04-29T21:39:56.677 回答