Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
393 views
in Technique[技术] by (71.8m points)

smlnj - How to share datatype declaration with SML and CM

I am a bit confused about using definitions from one SML file in another SML file in a SML project that I compile with CM. It seems like A.sml can only use signature and structure definitions from B.sml if they have been declared in the project.cm file.

I want to declare a datatype MyType = MyConstr {name:string, other: string} in B.sml, and construct values of that type MyType in A.sml. If I understand the CM docs right, I can only export structures, signatures, functors and functor signatures. This sounds fairly limiting in terms of code reuse.

Now, does that mean that my MyType would need to be part of a structure of signature, so that I can use it from another module?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You're correct that (at the top level) you can only export modules, but this doesn't mean you can only use signature and structure definitions in one file if they're described in a .cm file---just that they need be mentioned there either directly or indirectly via source(-) (or similar) to be used externally to the library.

Supposing you have some module in A.sml that you'd like to use in B.sml and your only goal is to expose structure Foo from B.sml, then something like

Library
    structure Foo
is
    A.sml
    B.sml

should suffice. You may also want to read sections 2.5 and 2.6 of the CM documentation (pp. 8-9) if you haven't already.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...