I am not sure about what to do with introspection in SML. It seems
that introspection capabilities are there, since the interactive
prompt is able to print the signature of any object, but they are
somewhat not exposed to the programmer. Case in point: I was thinking
about writing a test runner (*). My idea was to collect tests in
structures; each test would have a name starting with "test" and a
signature unit->bool, returning true if the test passed and false
otherwise. The runner should be able to execute all the functions in
the structure matching the signature and with a name starting with
"test". This would be trivial to do in a language with introspection;
but in SML I don't know how to proceed. I can think of various
solutions, but I am not happy with them. One possibility is to parse
the source code with a regular expression, to find the tests and to
generate the source code for the runner, but it certainly does not
look clean; another solution is to register the test names in a list,
but that requires to add a registration call for each test and
duplicating the names, in that case I would better off just calling
the tests directly in the runner. This is disturbing, since each time
I add a test I must change the runner, and if I change the name of a
test I have to change it twice. I could go with an association
list like the following:
testList = [
("testOnePlusOne", fn () => 1+1=2),
("testTwoPlusTwo", fn () => 2+2=4)
]
bit it just looks ugly compared to
fun testOnePlusOne = 1+1=2
fun testTwoPlusTwo = 2+2=4
The most disturbing thing is that the compiler already knows all the
names
in a structure: why I cannot extract them from the signature? It looks
like a serious restriction to me (for instance, how do you write
do***entation
tools without introspection, expecially if you have only the compiled
form of a library?) but maybe there is some trick I am not aware of.
Please illuminate me!
(*) I am not really going into writing it. It is just an example of
a program where I would use introspection.


|