Search code examples
smlsmlnj

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?


Solution

  • 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.