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