3
我有一个描述OrderItem
记录类型:`Nothing`不匹配类型的`一个记录字段也许A`
type alias Product {- = some record type -}
type alias Quantity = Int
type alias OrderItem = {
product: Product
, quantity: Quantity
, fillable: Maybe Bool
}
执行一个产品查找功能:
productByCode : String -> Maybe Product
和
orderItem : String -> Quantity -> Result OrderItem
orderItem productCode quantity =
let
product = productByCode productCode
in
case product of
Just p -> Ok { product = p, quantity = quantity, fillable = Nothing }
Nothing -> Err ("Unknown product code: " ++ productCode)
但是这个功能将导致编译错误:即建立一个OrderItem的功能
-- TYPE MISMATCH ---------------------------------------------- src/elm/Main.elm The type annotation for `orderItem` does not match its definition. 65│ orderItem : String -> Quantity -> Result OrderItem ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ The type annotation is saying: String -> Quantity -> Result OrderItem But I am inferring that the definition has this type: String -> Quantity -> Result String { fillable : Maybe a, product : Product, quantity : Quantity }
错误告诉我,编译器不匹配,因为Nothing
不匹配Maybe Bool
的OrderItem的类型 - 但肯定Nothing
这里是一个有效的价值?该文件似乎允许以这种方式使用它。
我哪里出错了?
当然的。傻我! – andrewdotnich