Check if a function is defined in SML
Given the original SML file, is it possible to check (using Poly / ML) if the list of function / value names is defined? If so, how?
Alternatively, I noticed that you can do the following. Suppose we have a source file to check, named somefile.sml
. Suppose we create a file test.sml
with the following content:
use "somefile"
f; (* defined in somefile.sml *)
g; (* undefined *)
And then we run:
use "test" handle e as SyntaxError => (print (exnMessage e));
Unfortunately, this only prints "Static Errors". Is there a way like the one above to identify (in code) that doesn't function in "test.sml"?
source to share
There is probably no way to make it portable, but in Poly / ML you can find out if a value is defined or something else using PolyML.globalNameSpace.
To check a value (like a function) use
#lookupVal PolyML.globalNameSpace
This takes a name and returns the type of the parameter, which is SOME if the value is defined and NONE if it was not. So
#lookupVal PolyML.globalNameSpace "f";
will return
SOME ?
and
#lookupVal PolyML.globalNameSpace "g";
will return NONE.
source to share