Based on a recent exchange, I’ve been convinced to use Template Haskell to generate some code to ensure compile-time type safety.
I need to introspect record field names and types. I understand I can get field names by using constrFields . toConstr :: Data a => a -> [String]. But I need more than the field names, I need to know their type. For example, I need to know the names of fields that are of type Bool.
How do I construct a function f :: a -> [(String, xx)] where a is the record, String is the field name and xx is the field type?
The type should be available, along with everything else, in the
Infovalue provided byreify. Specifically, you should get aTyConI, which contains aDecvalue, from which you can get the list ofConvalues specifying the constructors. A record type should then useRecC, which will give you a list of fields described by a tuple containing the field name, whether the field is strict, and the type.Where you go from there depends on what you want to do with all this.
Edit: For the sake of actually demonstrating the above, here’s a really terrible quick and dirty function that finds record fields:
Importing that in another module, we can use it like so:
Loading that in GHCi, the value in
foois[("Foo",[("foo1","ConT GHC.Types.Int"),("foo2","ConT GHC.Types.Bool")])].Does that give you the rough idea?