如何在F#中编写枚举而不显式分配数字文字? [英] How do I write an enumeration in F# without explicitly assigning number literals?

查看:88
本文介绍了如何在F#中编写枚举而不显式分配数字文字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在F#中有一个枚举,如下所示:

  type Creature = 
| SmallCreature = 0
| MediumCreature = 1
| GiantCreature = 2
| HumongousCreature = 3
| CreatureOfNondescriptSize = 4

我不喜欢手动输出数字,我想轻松地插入更多的项目枚举后,不必转移数字。



我尝试过这个

 类型生物= 
| SmallCreature
| MediumCreature
| GiantCreature
| HumongousCreature
| CreatureOfNondescriptSize

但它导致错误类型生物不是CLI枚举类型稍后程序

  let input = Int32.Parse(Console.ReadLine()) 
let output = match EnumOfValue< int,Creature>(input)with //< ---出现错误
| Creature.SmallCreature - > 老鼠
| Creature.MediumCreature - > 狗
| Creature.GiantCreature - > 大象
| Creature.HumongousCreature - > 鲸鱼
| Creature.CreatureOfNondescriptSize - > Jon Skeet
| _ - > unacceptably Hideous Monstrosity

Console.WriteLine(output)
Console.WriteLine()
Console.WriteLine(按任意键退出...)
Console.Read()|>忽略

如何定义枚举而不手动为每个项目分配数值?

解决方案

不幸的是,你不能。数值是否重要?如果是这样,它有点逃避了枚举的使用(标志旁边)。在这种情况下,您可能会考虑一个阶级或歧视性的联盟。



您的第二个例子实际上是一个歧视的联盟。但是,您以后使用 EnumOfValue (期望枚举)会导致错误。



另一个选项是存储枚举到字典中的数字映射,并替换与字典查找匹配的模式。然后,枚举的数值将是无关紧要的。



我同意手动管理枚举值是繁琐的。我希望在未来的版本中解决。


I have an enumeration in F#, like this:

type Creature =
   | SmallCreature = 0
   | MediumCreature = 1
   | GiantCreature = 2
   | HumongousCreature = 3
   | CreatureOfNondescriptSize = 4

I do not like manually typing out numbers, and I want to easily insert more items in the enumeration later without having to shift the numbers.

I tried this

type Creature =
   | SmallCreature
   | MediumCreature
   | GiantCreature
   | HumongousCreature
   | CreatureOfNondescriptSize

but it caused an error The type 'Creature' is not a CLI enum type later in the program

let input = Int32.Parse(Console.ReadLine())
let output = match EnumOfValue<int, Creature>(input) with // <---Error occurs here
    | Creature.SmallCreature -> "Rat"
    | Creature.MediumCreature -> "Dog"
    | Creature.GiantCreature -> "Elephant"
    | Creature.HumongousCreature -> "Whale"
    | Creature.CreatureOfNondescriptSize -> "Jon Skeet"
    | _ -> "Unacceptably Hideous Monstrosity"

Console.WriteLine(output)
Console.WriteLine()
Console.WriteLine("Press any key to exit...")
Console.Read() |> ignore

How can I define an enumeration without manually assigning number values to each item?

解决方案

Unfortunately, you can't. Are the numeric values important? If so, it somewhat escapes the intended use of enums (flags aside). You might consider a class or discriminated union in that case.

Your second example is, in fact, a discriminated union. But your later use of EnumOfValue, which expects an enum, is causing the error.

Another option is to store the enum to number mapping in a dictionary and replace the pattern matching with a dictionary lookup. Then, the numeric value of the enum would be irrelevant.

I agree that manually managing enum values is burdensome. I hope it's addressed in a future version.

这篇关于如何在F#中编写枚举而不显式分配数字文字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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