1

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

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.