类型不匹配(对于自定义类型)
Type Mismatch (for a custom type)
与我用普通英语解释相比,我认为代码更能说明问题。
这是域建模的代码:
module Domain.A exposing (..)
import Domain.B
type Size
= Pound Float
| Kilogram Float
| Gram Float
| Liter Float
| Ounce Float
| Count Int
type StockStatus
= InStock
| OutOfStock
type alias CityWholesalerProduct =
{
cityWholesalerId : String,
brand : String,
productSku : Maybe String,
sku : String,
upc : Maybe String,
description : String,
status : StockStatus,
price : Float,
unit : Domain.B.Unit,
pack : Int,
size : Size
}
这是Main.elm
中模型的代码
-- MODEL
type alias Model =
{ cityWholesalerProducts : (List Domain.A.CityWholesalerProduct)
, cityWholesalerProduct : Domain.A.CityWholesalerProduct
}
init : Model
init =
Model
[]
{
cityWholesalerId = "",
brand = "",
productSku = "",
sku = "",
upc = "",
description = "",
status = Domain.A.OutOfStock,
price = 0.00,
unit = Domain.B.Count,
pack = 0,
size = Domain.A.Count 0
}
问题是我收到一条错误消息:
TYPE MISMATCH - The 2nd argument to `Model` is not what I expect:
34| Model
35| []
36|#># {
37|#># cityWholesalerId = "",
38|#># brand = "",
39|#># productSku = "",
40|#># sku = "",
41|#># upc = "",
42|#># description = "",
43|#># status = Domain.A.OutOfStock,
44|#># price = 0.00,
45|#># unit = Domain.B.Count,
46|#># pack = 0,
47|#># size = Domain.A.Count 0
48|#># }
This argument is a record of type:
{ brand : String
, cityWholesalerId : String
, description : String
, pack : Int
, price : Float
, productSku : #String#
, size : Domain.A.Size
, sku : String
, status : Domain.A.StockStatus
, unit : Domain.B.Unit
, upc : #String#
}
But `Model` needs the 2nd argument to be:
#Domain.A.CityWholesalerProduct#
#Hint#: I always figure out the argument types from left to right. If an argument
is acceptable, I assume it is “correct” and move on. So the problem may actually
be in one of the previous arguments!
总的来说,我是 Elm 和 FP 的新手,类型不匹配错误是我最大的痛点,我已经在这个问题上停留了一段时间。
令我惊讶的是,Elm 编译器(通常会提供非常有用的错误消息)在这里没有更具体。但我看到的问题只是在那些领域 - productSku
和 upc
- 你的类型别名说它们应该是 Maybe String
类型,但你已经用值 ""
初始化了], 这只是一个 String
.
不清楚哪一部分是错误的。如果那些总是应该是简单的字符串,那么修复类型声明,在那些地方将 Maybe String
更改为 String
。但我假设类型声明可能如您所愿,并且值是错误的。您可以试试这个:
init =
Model
[]
{
cityWholesalerId = "",
brand = "",
productSku = Just "",
sku = "",
upc = Just "",
description = "",
status = Domain.A.OutOfStock,
price = 0.00,
unit = Domain.B.Count,
pack = 0,
size = Domain.A.Count 0
}
这至少应该编译 - 但我无法评论您预期的业务逻辑的正确值是什么。我不清楚 Nothing
对这些 Maybe
值意味着什么,以及这是否真的与 Just ""
有任何不同 - 但很可能它们是。
与我用普通英语解释相比,我认为代码更能说明问题。
这是域建模的代码:
module Domain.A exposing (..)
import Domain.B
type Size
= Pound Float
| Kilogram Float
| Gram Float
| Liter Float
| Ounce Float
| Count Int
type StockStatus
= InStock
| OutOfStock
type alias CityWholesalerProduct =
{
cityWholesalerId : String,
brand : String,
productSku : Maybe String,
sku : String,
upc : Maybe String,
description : String,
status : StockStatus,
price : Float,
unit : Domain.B.Unit,
pack : Int,
size : Size
}
这是Main.elm
中模型的代码-- MODEL
type alias Model =
{ cityWholesalerProducts : (List Domain.A.CityWholesalerProduct)
, cityWholesalerProduct : Domain.A.CityWholesalerProduct
}
init : Model
init =
Model
[]
{
cityWholesalerId = "",
brand = "",
productSku = "",
sku = "",
upc = "",
description = "",
status = Domain.A.OutOfStock,
price = 0.00,
unit = Domain.B.Count,
pack = 0,
size = Domain.A.Count 0
}
问题是我收到一条错误消息:
TYPE MISMATCH - The 2nd argument to `Model` is not what I expect:
34| Model
35| []
36|#># {
37|#># cityWholesalerId = "",
38|#># brand = "",
39|#># productSku = "",
40|#># sku = "",
41|#># upc = "",
42|#># description = "",
43|#># status = Domain.A.OutOfStock,
44|#># price = 0.00,
45|#># unit = Domain.B.Count,
46|#># pack = 0,
47|#># size = Domain.A.Count 0
48|#># }
This argument is a record of type:
{ brand : String
, cityWholesalerId : String
, description : String
, pack : Int
, price : Float
, productSku : #String#
, size : Domain.A.Size
, sku : String
, status : Domain.A.StockStatus
, unit : Domain.B.Unit
, upc : #String#
}
But `Model` needs the 2nd argument to be:
#Domain.A.CityWholesalerProduct#
#Hint#: I always figure out the argument types from left to right. If an argument
is acceptable, I assume it is “correct” and move on. So the problem may actually
be in one of the previous arguments!
总的来说,我是 Elm 和 FP 的新手,类型不匹配错误是我最大的痛点,我已经在这个问题上停留了一段时间。
令我惊讶的是,Elm 编译器(通常会提供非常有用的错误消息)在这里没有更具体。但我看到的问题只是在那些领域 - productSku
和 upc
- 你的类型别名说它们应该是 Maybe String
类型,但你已经用值 ""
初始化了], 这只是一个 String
.
不清楚哪一部分是错误的。如果那些总是应该是简单的字符串,那么修复类型声明,在那些地方将 Maybe String
更改为 String
。但我假设类型声明可能如您所愿,并且值是错误的。您可以试试这个:
init =
Model
[]
{
cityWholesalerId = "",
brand = "",
productSku = Just "",
sku = "",
upc = Just "",
description = "",
status = Domain.A.OutOfStock,
price = 0.00,
unit = Domain.B.Count,
pack = 0,
size = Domain.A.Count 0
}
这至少应该编译 - 但我无法评论您预期的业务逻辑的正确值是什么。我不清楚 Nothing
对这些 Maybe
值意味着什么,以及这是否真的与 Just ""
有任何不同 - 但很可能它们是。