3

I would like to know whether there would be a way to print the name of the constructor of a datatype from a function in SML. I could find ways to do this in Haskell by deriving the datatype from libraries like show but not in SML.
I need this because I have a very large datatype with lots of constructors which needs to pretty print the data from a function.
Hence the below code wouldn't work for me.

fun print (Cons of var) = "cons"
blueconch
  • 31
  • 2
  • 3
    There is no such feature. Perhaps you could use a preprocessor or script and generate the tedious bits? – molbdnilo Feb 17 '22 at 16:24
  • There's also a decent chance this is an [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). – Chris Feb 18 '22 at 21:08
  • What I usually do is just define my own `toString` function and use that. Alternatively, I may just directly call the helper function straight from the REPL and it will print out a string representation even if I don't define `toString`. The stuff printed at the top-level can be customized for monotypes: https://github.com/jluningp/smlnj-obscure-tutorials/blob/master/pretty-printer.md – Ionuț G. Stan Mar 16 '22 at 17:43

0 Answers0