2016-02-19 36 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

相关问题