如何枚举F#中受歧视的工会? [英] How to enumerate a discriminated union in F#?

查看:79
本文介绍了如何枚举F#中受歧视的工会?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何枚举F#中受歧视的并集的可能值"?

How can I enumerate through the possible "values" of a discriminated union in F#?

我想知道是否存在类似于Enum.GetValues(Type)的受歧视工会,很难确定要枚举的数据类型.我想为每个选项生成一个带有一个项的已区分联合的列表或数组.

I want to know if is there something like Enum.GetValues(Type) for discriminated unions, tough I am not sure over what kind of data I would enumerate. I would like to generate a list or array of a discriminated union with one item for each option.

推荐答案

是的,F#在.NET的反射之上具有自己的反射层,以帮助您理解F#特定的类型,例如区分联合.这是使您枚举工会案件的代码:

Yes, F# has it's own reflection layer build on top of .NET's reflection to help you make sense of types that are specific to F#, like discriminating unions. Here's the code that will let you enumerate a union's cases:

open Microsoft.FSharp.Reflection

type MyDU =
    | One
    | Two
    | Three

let cases = FSharpType.GetUnionCases typeof<MyDU>

for case in cases do printfn "%s" case.Name

这篇关于如何枚举F#中受歧视的工会?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆