Elm 记录 shorthand 表示法
Elm record shorthand notation
为了在 JavaScript 中更好地进行函数式编程,我开始学习 Elm。
在 JavaScript 中你有对象 shorthand 符号:
const foo = 'bar';
const baz = { foo }; // { foo: 'bar' };
我想知道 Elm 中是否有类似的东西?我问是因为我有以下型号:
type alias Model =
{ title : String
, description : String
, tag : String
, tags : List String
, notes : List Note
}
type alias Note =
{ title : String
, description : String
, tags : List String
}
还有一个 update
函数,它在收到 AddNote
操作后将注释添加到注释数组并清除输入:
AddNote ->
{ model | notes = model.notes ++ [ { title = model.title, description = model.description, tags = model.tags } ], title = "", description = "", tag = "" }
我知道在函数定义中你可以 "destructure" 记录,但我认为即使在 return 类型中我也必须明确地键入记录的每个键。
AddNote ->
{ model | notes = model.notes ++ [ getNote model ], title = "", description = "", tag = "" }
getNote : Model -> Note
getNote { title, description, tags } =
{ title = title, description = description, tags = tags }
没有类似于 JavaScript 对象的 shorthand 记录符号。
但是,类型别名也可以用作构造函数,因此您可以这样:
getNote : Model -> Note
getNote { title, description, tags } =
Note title description tags
为了在 JavaScript 中更好地进行函数式编程,我开始学习 Elm。
在 JavaScript 中你有对象 shorthand 符号:
const foo = 'bar';
const baz = { foo }; // { foo: 'bar' };
我想知道 Elm 中是否有类似的东西?我问是因为我有以下型号:
type alias Model =
{ title : String
, description : String
, tag : String
, tags : List String
, notes : List Note
}
type alias Note =
{ title : String
, description : String
, tags : List String
}
还有一个 update
函数,它在收到 AddNote
操作后将注释添加到注释数组并清除输入:
AddNote ->
{ model | notes = model.notes ++ [ { title = model.title, description = model.description, tags = model.tags } ], title = "", description = "", tag = "" }
我知道在函数定义中你可以 "destructure" 记录,但我认为即使在 return 类型中我也必须明确地键入记录的每个键。
AddNote ->
{ model | notes = model.notes ++ [ getNote model ], title = "", description = "", tag = "" }
getNote : Model -> Note
getNote { title, description, tags } =
{ title = title, description = description, tags = tags }
没有类似于 JavaScript 对象的 shorthand 记录符号。
但是,类型别名也可以用作构造函数,因此您可以这样:
getNote : Model -> Note
getNote { title, description, tags } =
Note title description tags