【问题标题】:How to import from another file in SML, with a path relative to the importer?如何使用相对于导入器的路径从 SML 中的另一个文件导入?
【发布时间】:2018-11-08 17:50:48
【问题描述】:

我正在使用 SML/NJ,我需要在另一个文件 f2.sml 中使用某个文件 f1.sml 中的一组函数。

但是,我没有直接运行f2.sml,而是从其他地方导入它。

如果我在f2.sml 中使用use 命令和f1.sml 相对于f2.sml 透视图的路径,当我导入f2.sml 时,它将从运行脚本透视图查找提供的路径.

我不能使用绝对路径,我不想合并两个文件的内容。

对不起,如果这是该语言的一个简单应用,但我是 SML 新手,还找不到答案。

【问题讨论】:

    标签: sml smlnj


    【解决方案1】:

    我建议使用SML/NJ's Compilation Manager (CM)。它是 SML/NJ 上下文中 SML 代码的构建系统。如果您需要more advanced features,它可能会变得相当复杂,但它很容易上手。我将向您展示一个准系统结构,您可以根据需要进行调整。它已经随 SML/NJ 一起安装了,所以没有安装过程。

    我将在此示例中使用以下目录结构(不强制文件扩展名,只是一个约定):

    .
    ├── build.cm
    └── src
        ├── foo.fun
        ├── foo.sig
        └── main.sml
    

    build.cm

    group
      (* CM allows you to selectively export defined modules (structures,
         signatures and functors) by listing them here. It's useful for
         libraries. *)
    
      source (-)       (* export all defined modules *)
    
      structure Main   (* OR, export selectively *)
      signature FOO
      functor Foo
    is
      (* Import the SML standard library, aka Basis.  *)
      (* See: http://sml-family.org/Basis/ *)
      $/basis.cm
    
      (* Import the SML/NJ library *)
      (* Provides extra data structures and algorithms. *)
      (* See: https://www.smlnj.org/doc/smlnj-lib/Manual/toc.html *)
      $/smlnj-lib.cm
    
      (* List each source file you want to be considered for compilation. *)
      src/main.sml
      src/foo.sig
      src/foo.fun
    

    src/main.sml

    structure Main =
      struct
        (* You don't have to import the `Foo` functor. *)
        (* It's been done in build.cm already. *)
        structure F = Foo()
    
        fun main () =
          print (F.message ^ "\n")
      end
    

    src/foo.sig

    signature FOO =
      sig
        val message : string
      end
    

    src/foo.fun

    (* You don't have to import the `FOO` signature. *)
    (* It's been done in build.cm already. *)
    functor Foo() : FOO =
      struct
        val message = "Hello, World!"
      end
    

    用法

    结构就绪后,您可以使用CM.make 开始编译并通过调用您定义的任何函数来运行:

    $ sml
    Standard ML of New Jersey v110.82 [built: Tue Jan  9 20:54:02 2018]
    - CM.make "build.cm";
    val it = true : bool
    -
    - Main.main ();
    Hello, World!
    val it = () : unit
    

    【讨论】:

    • 优秀的答案。
    • 谢谢@SimonShine。
    • 这一切都很好等等,但它并没有真正回答如何导入另一个文件相对于正在编辑的文件的问题。你能补充一下这些信息吗?也许在评论中,您说不需要导入,因为它已经在build.cm 中? (例如:如果一个模块依赖于第二个模块,我该怎么办?不知何故,它需要在第一个模块中引用。(我猜?))
    • @ZelphirKaltstahl 只要您在 CM 文件中列出所有模块,您就可以从任何模块(条形循环依赖项)中引用任何模块。在该文件中列出它们基本上会使模块层次结构变平。您可以在 CM 文件中使用任何文件系统路径,但在 SML 文件中,您只需使用模块名称即可。有意义吗?
    猜你喜欢
    • 2015-09-25
    • 2020-10-23
    • 1970-01-01
    • 2017-07-01
    • 1970-01-01
    • 2021-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多