Question:
I would like to generate a HTML form from types in Haskell.
Color = Green | Yellow | Red | ....
Fruit = Apple Color Int
| Banana Color Int String
| ....
User shall pick a dropdown list with option of ("Apple","Banana") or ("Green","Yellow","Red")
Question:
But how to get ALL
values of type Fruit
/Color
values to HTML ? something like:
[ toHtml v <- Fruit.values() ]
-- render "Apple" to html
-- render "Banana" to html
expected:
<dropdown> : values :apple/banana...
<dropdown> : values of green/yellow...
<input text> : for Int
<input text> : for String(if Banana was selected
The Data
class helps here, e.g
data Color = Green | Yellow | Red deriving Data
colorStrings :: [String]
colorStrings = map show $ dataTypeConstrs (dataTypeOf Green :: DataType)
To get Fruit
where data constructors also have fields you'd have to work harder but the required info should be available within the DataType
data type.