2016-02-19 64 views
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這裏是一個有效的價值?該文件似乎允許以這種方式使用它。

我哪裏出錯了?

回答

2

類型註釋應該是:

orderItem : String -> Quantity -> Result String OrderItem 

這告訴編譯器,該值要麼是Ok OrderItem類型或Err String

+0

當然的。傻我! – andrewdotnich

相關問題