我有一个清单
type my_sum = {a : type_a} / {b : type_b}
mylist = [{field_only_in_a = "test"} : type_a,{haha=3 ; fsd=4} : type_b]
我想这样做:
result = List.find( a -> match a with
| {a = _} -> true
| _ -> false
end,
mylist)
if Option.is_some(result) then
Option.some(Option.get(result).field_only_in_a)
else
Option.none
正如你所看到的,在找到之后我肯定会得到一些东西,type_a
但在编译时我得到了:
Record has type
{ a : type_a } / { b : type_b } but field access expected it to have type
{ field_only_in_a: 'a; 'r.a }
Hint:
You tried to access a sum type with several cases as a record.
我怎么能对编译器说我只提取了一种类型的 sum 类型并且我有好的类型来访问记录...?