如何从SML中的另一个文件导入,并具有相对于导入器的路径?
我使用的是SML/NJ,我需要使用一组函数,这些函数位于另一个文件中的某个文件如何从SML中的另一个文件导入,并具有相对于导入器的路径?,sml,smlnj,Sml,Smlnj,我使用的是SML/NJ,我需要使用一组函数,这些函数位于另一个文件中的某个文件f1.SML 但是,我不是直接运行f2.sml,而是从其他地方导入它 如果我使用f2.sml中的use命令,将路径设置为相对于f2.sml透视图的f1.sml,则在导入f2.sml时,它将从运行脚本透视图中查找提供的路径 我不能使用绝对路径,我不想合并这两个文件的内容 如果这是一个简单的语言应用程序,很抱歉,但我是SML新手,还没有找到答案。我建议使用(CM)。它是SML/NJ上下文中SML代码的构建系统。如果需要,它
f1.SML
但是,我不是直接运行f2.sml,而是从其他地方导入它
如果我使用f2.sml
中的use
命令,将路径设置为相对于f2.sml
透视图的f1.sml
,则在导入f2.sml
时,它将从运行脚本透视图中查找提供的路径
我不能使用绝对路径,我不想合并这两个文件的内容
如果这是一个简单的语言应用程序,很抱歉,但我是SML新手,还没有找到答案。我建议使用(CM)。它是SML/NJ上下文中SML代码的构建系统。如果需要,它可能会变得相当复杂,但很容易开始。我将向您展示一个裸骨结构,您可以根据需要进行调整。它已经安装了SML/NJ,因此没有安装过程
在本例中,我将使用以下目录结构(文件扩展名不是强制的,只是一种约定):
build.cm
src/main.sml
src/foo.sig
src/foo.fun
用法
结构就绪后,您可以使用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。
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
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
signature FOO =
sig
val message : string
end
(* 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
$ 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