因此,我尝试创建具有区别的联合类型的列表,例如;
type ColType = Int of int | Float of float | String of string
然后插入到列表中,例如
let addToList (list : ColType list) (col : ColType) =
let list' = list @[col]
list'
但是我不确定如何初始化coltype值,因为我只能获取int-> coltype等值。
我尝试了此功能
let createColType x =
if x.GetType() = int then
Int x
else if x.GetType() = float then
Float x
else if x.GetType() = string then
String x
else
String x
这显然不起作用,因为它将返回不同的值,那么您将如何解决呢?
答案 0 :(得分:1)
使用match
检查多个选项,并使用:?
匹配类型:
let createColType x =
match box x with
| :? int as i -> ColType.I i
| :? float as f -> ColType.F f
| :? string as s -> ColType.S s
|_-> failwithf "Type not supported %A" <| x.GetType().FullName
createColType 1 |> printfn "%A" // shows: I 1
createColType 2. |> printfn "%A" // shows: F 2.0
createColType "3" |> printfn "%A" // shows: S "3"
答案 1 :(得分:0)
type Food = Hamburgers of int | Soda of float | Other of string
module Food =
let tryCreate food =
match box food with
| :? int as i -> Some(Hamburgers i)
| :? float as f -> Some(Soda f)
| :? string as s -> Some(Other s)
| _ -> None
let burgers = Hamburgers 7
let soda = Soda 3.2
let mozzarellaSticks = Other "Mozzarella Sticks"
let mysteryMeat = Option.toList(Food.tryCreate "nobody knows")
let list = [burgers; soda; mozzarellaSticks] @ mysteryMeat
通过使用Option
作为tryCreate
的返回类型,我不会得到任何运行时异常。您还会注意到,我尝试创建与我的业务目标相关的DU标签。这使表达意图更容易,并且使联合比简单的int,字符串和浮点数更有用。大多数时候,我知道我拥有哪种类型,因为该类型与业务用途相对应,因此我不必编写或使用tryCreate。在实践中,通常无法将基元映射到我们所区分的联合,例如,考虑是否添加了| Hotdogs of int
,如果添加的int是热狗还是汉堡包,那就模棱两可。