【发布时间】:2017-01-01 13:35:14
【问题描述】:
我正在使用 elm-form:https://github.com/etaque/elm-form/,我需要将多态类型模式匹配为具体类型,但出现下一个错误:
The pattern matches things of type:
TranslationId
But the values it will actually be trying to match are:
e
Hint: Your type annotation uses type variable `e` which means any type of value
can flow through. Your code is saying it CANNOT be anything though! Maybe change
your type annotation to be more specific? Maybe the code has a problem? More at:
<https://github.com/elm-lang/elm-compiler/blob/0.18.0/hints/type-annotations.md>
有问题的代码是这样的:
translateError : ErrorValue e -> String
translateError error =
case error of
InvalidEmail ->
translate English ErrInvalidEmail
Empty ->
translate English ErrEmpty
CustomError PasswordNotMatch ->
translate English PasswordNotMatch
x ->
toString x
type ErrorValue e
= Empty
| InvalidString
| InvalidEmail
| InvalidUrl
| InvalidFormat
| InvalidInt
| InvalidFloat
| InvalidBool
| InvalidDate
| SmallerIntThan Int
| GreaterIntThan Int
| SmallerFloatThan Float
| GreaterFloatThan Float
| ShorterStringThan Int
| LongerStringThan Int
| NotIncludedIn
| CustomError e
TranslationId 类型https://github.com/werner/madison-elm/blob/master/src/elm/Translations/Utils.elm#L9:
type TranslationId
= ErrInvalidEmail
| PasswordNotMatch
| ErrEmpty
我想出了一个解决方案,但它看起来很奇怪,我不确定它是否正确https://github.com/werner/madison-elm/blob/master/src/elm/Translations/FormErrors.elm#L7:
translateError : ErrorValue e -> String
translateError error =
case error of
InvalidEmail ->
translate English ErrInvalidEmail
Empty ->
translate English ErrEmpty
CustomError e ->
case (toString e) of
"PasswordNotMatch" ->
translate English PasswordNotMatch
x ->
toString x
x ->
toString x
【问题讨论】:
-
那是因为您没有指定
e在translateError : ErrorValue e -> String中的内容 - 所以它对此一无所知。也许传入e -> String函数?
标签: elm