Elm 可扩展记录初始化
Elm extensible records init
我正在尝试创建一个装饰器类型来保存某些项目周围的状态。该项目可以是电子邮件、新闻公告、提醒等...我认为这样做可以,但我遇到了错误。
type alias Model a =
{a | pinned : Bool, done : Bool, trunc : Bool}
init : a -> Model(a)
init cont = {cont | pinned <- False, done <- False, trunc <- False}
这是编译错误:
Detected errors in 1 module.
## ERRORS in .\.\Item.elm ######################################################
-- TYPE MISMATCH -------------------------------------------------- .\.\Item.elm
The type annotation for `init` does not match its definition.
20| init : a -> Model(a)
^^^^^^^^^^^^^
Could not unify user provided type variable `a`. The most likely cases are:
1. The type you wrote down is too general. It says any type can go through
but in fact only a certain type can.
2. A type variable is probably being shared between two type annotations.
They are treated as different things in the current implementation, so
Try commenting out some type annotations and see what happens.
As I infer the type of values flowing through your program, I see a conflict
between these two types:
a
{ d | done : a, pinned : b, trunc : c }
我要装饰的类型例如:
type alias Email =
{ from: String
, to: String
, title: String
, body: String
, date: String
}
type alias Reminder =
{ body: String
, created: String
}
将“<-”更改为“=”后,出现以下模糊的语法错误:
Detected errors in 1 module.
## ERRORS in .\.\Item.elm ######################################################
-- SYNTAX PROBLEM ------------------------------------------------- .\.\Item.elm
I ran into something unexpected when parsing your code!
21| init cont = { cont | pinned = False, done = False, trunc = False }
^
I am looking for one of the following things:
a closing bracket '}'
a field access like .name
an expression
an infix operator like (+)
more letters in this name
whitespace
我从您使用的语法中看出这不是 Elm 0.16,这是记录更新语法发生变化的版本。在您当前的 Elm 版本中,您可以执行以下操作:
type alias Model a =
{a | pinned : Bool, done : Bool, trunc : Bool}
init : a -> Model a -- the parentheses were superfluous so I removed them
init cont0 =
let cont1 = { cont0 | pinned = False }
cont2 = { cont1 | done = False }
cont3 = { cont2 | trunc = False }
in cont3
因为根据类型注释,您是在添加 字段,而不是更新 字段。遗憾的是,您一次只能添加一个字段,所以请原谅笨拙的语法。
请注意,在 Elm 中 0.16 field addition syntax has been removed。它使用不多,新手发现 =
与 <-
非常混淆。因此删除了字段添加,并将更新语法更改为使用 =
。这意味着在新版本的 Elm 中,您将无法再以这种方式编写 init 函数。请参阅博客 post 和邮件列表,了解使用可扩展记录重写程序以使用其他类型建模的策略。
举一个例子,说明您可以如何更改您的程序以仍然使用 Elm 0.16+:
type alias Model a =
{ submodel: a, pinned: Bool, done: Bool, trunc: Bool }
init : a -> Model a
init submodel = Module submodel False False False
-- This makes use of the generated record constructor
-- that came with the type alias definition.
-- You could also write:
-- { submodel: submodel, pinned: False, done: False, trunc: False }
我正在尝试创建一个装饰器类型来保存某些项目周围的状态。该项目可以是电子邮件、新闻公告、提醒等...我认为这样做可以,但我遇到了错误。
type alias Model a =
{a | pinned : Bool, done : Bool, trunc : Bool}
init : a -> Model(a)
init cont = {cont | pinned <- False, done <- False, trunc <- False}
这是编译错误:
Detected errors in 1 module.
## ERRORS in .\.\Item.elm ######################################################
-- TYPE MISMATCH -------------------------------------------------- .\.\Item.elm
The type annotation for `init` does not match its definition.
20| init : a -> Model(a)
^^^^^^^^^^^^^
Could not unify user provided type variable `a`. The most likely cases are:
1. The type you wrote down is too general. It says any type can go through
but in fact only a certain type can.
2. A type variable is probably being shared between two type annotations.
They are treated as different things in the current implementation, so
Try commenting out some type annotations and see what happens.
As I infer the type of values flowing through your program, I see a conflict
between these two types:
a
{ d | done : a, pinned : b, trunc : c }
我要装饰的类型例如:
type alias Email =
{ from: String
, to: String
, title: String
, body: String
, date: String
}
type alias Reminder =
{ body: String
, created: String
}
将“<-”更改为“=”后,出现以下模糊的语法错误:
Detected errors in 1 module.
## ERRORS in .\.\Item.elm ######################################################
-- SYNTAX PROBLEM ------------------------------------------------- .\.\Item.elm
I ran into something unexpected when parsing your code!
21| init cont = { cont | pinned = False, done = False, trunc = False }
^
I am looking for one of the following things:
a closing bracket '}'
a field access like .name
an expression
an infix operator like (+)
more letters in this name
whitespace
我从您使用的语法中看出这不是 Elm 0.16,这是记录更新语法发生变化的版本。在您当前的 Elm 版本中,您可以执行以下操作:
type alias Model a =
{a | pinned : Bool, done : Bool, trunc : Bool}
init : a -> Model a -- the parentheses were superfluous so I removed them
init cont0 =
let cont1 = { cont0 | pinned = False }
cont2 = { cont1 | done = False }
cont3 = { cont2 | trunc = False }
in cont3
因为根据类型注释,您是在添加 字段,而不是更新 字段。遗憾的是,您一次只能添加一个字段,所以请原谅笨拙的语法。
请注意,在 Elm 中 0.16 field addition syntax has been removed。它使用不多,新手发现 =
与 <-
非常混淆。因此删除了字段添加,并将更新语法更改为使用 =
。这意味着在新版本的 Elm 中,您将无法再以这种方式编写 init 函数。请参阅博客 post 和邮件列表,了解使用可扩展记录重写程序以使用其他类型建模的策略。
举一个例子,说明您可以如何更改您的程序以仍然使用 Elm 0.16+:
type alias Model a =
{ submodel: a, pinned: Bool, done: Bool, trunc: Bool }
init : a -> Model a
init submodel = Module submodel False False False
-- This makes use of the generated record constructor
-- that came with the type alias definition.
-- You could also write:
-- { submodel: submodel, pinned: False, done: False, trunc: False }