I want to get a list of data constructors in Idris 2. How is this done? This seems to work, but is there a better way to do this?
module GetConstructors
import Language.Reflection
%language ElabReflection
data Color = Red | Green | Blue
Show Color where
show Red = "Red"
show Green = "Green"
show Blue = "Blue"
colors : List Color
colors = %runElab do
let n = NS (MkNS ["GetConstructors"]) (UN $ Basic "Color")
conNames <- getCons n
logMsg "info" 1 $ "Got constructor names: " ++ show conNames
-- Need to convert the constructor names into actual values
let constructors = map (\n => IVar EmptyFC n) conNames
check $ `(the (List Color) ~(foldr (\x, acc => `(~(x) :: ~(acc)))
`([]) constructors))
main : IO ()
main = do
putStrLn "Colors:"
traverse_ (\c => putStrLn $ " " ++ show c) colors
asked Nov 23, 2024 at 1:45
Quaxton Hale
2,5506 gold badges45 silver badges74 bronze badges