如何使用相对于导入程序的路径从 SML 中的另一个文件导入?
How to import from another file in SML, with a path relative to the importer?
我正在使用 SML/NJ,我需要在另一个文件 f2.sml
.
中使用某个文件 f1.sml
中的一组函数
但是,我不是直接运行 f2.sml
,而是从其他地方导入它。
如果我在 f2.sml
中使用 use
命令,相对于 f2.sml
透视图 f1.sml
的路径,在我导入 f2.sml
时,它将从 运行 脚本角度寻找提供的路径。
我不能使用绝对路径,我不想合并这两个文件的内容。
抱歉,如果这是该语言的一个简单应用程序,但我是 SML 的新手,还找不到答案。
我推荐使用 SML/NJ's Compilation Manager (CM). It's a build system for SML code in the context of SML/NJ. It can get quite complicated if you need ,但它很容易上手。我将向您展示一个准系统结构,您可以根据需要进行调整。它已经与 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.有趣
(* 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
我正在使用 SML/NJ,我需要在另一个文件 f2.sml
.
f1.sml
中的一组函数
但是,我不是直接运行 f2.sml
,而是从其他地方导入它。
如果我在 f2.sml
中使用 use
命令,相对于 f2.sml
透视图 f1.sml
的路径,在我导入 f2.sml
时,它将从 运行 脚本角度寻找提供的路径。
我不能使用绝对路径,我不想合并这两个文件的内容。
抱歉,如果这是该语言的一个简单应用程序,但我是 SML 的新手,还找不到答案。
我推荐使用 SML/NJ's Compilation Manager (CM). It's a build system for SML code in the context of SML/NJ. It can get quite complicated if you need
我将在此示例中使用以下目录结构(不强加文件扩展名,只是约定):
.
├── 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.有趣
(* 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