admin管理员组文章数量:1122846
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
本文标签: reflectionGet list of data constructors in Idris 2Stack Overflow
版权声明:本文标题:reflection - Get list of data constructors in Idris 2 - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736300094a1930696.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论