Elm 记录更新与类型别名不匹配

Elm record updating mismatch with type alias

我有以下类型:

type alias SelList a =
    { list : List a
    , selected : Maybe a
    }

Sel(ectable)List a 是一个 a 的列表,我可以从中选择一个元素。

在我的应用程序中,我所有的对象都有一个 id : Int 字段,所以我定义了这个类型别名:

type alias HasId r = { r | id : Int}

现在我想要一个函数最终选择列表中的一个元素,我试过了:

select : Int -> SelList (HasId r)-> Maybe (SelList (HasId r))
select id sl = find (\x-> x.id ==id) sl.list &> \ el ->
               Just { sl | selected = el }

其中 (&>) = flip Maybe.andThenfind : (a -> Bool) -> List a -> Maybe a

我收到以下消息:

The type annotation for `select` says it always returns:

    Maybe (SelList (HasId r))

But the returned value (shown above) is a:

    Maybe { list : List (HasId r), selected : { r | id : Int } }

我很困惑,因为 { r | id : Int }HasId 相同,然后

{ list : List (HasId r), selected : HasId r }

SelList (HasId r) 相同。为什么编译器无法确定类型匹配?

90% 的编译器错误,但我认为混合类型别名和记录会使找出问题所在变得更加困难。 (Elm 的未来版本将对此进行改进)。

如果它是一条记录而不是 Maybe record,编译器会告诉您类似 "I see a problem with the selected field`" 的内容。有帮助吗?

剧透:SelList 类型有一个 selected : Maybe a,但是 select returns selected : a