Dam*_*n W 2 functional-programming elm
在Elm复选框示例中,将 an Action传递给函数的tag参数checkbox(第51-53行).
我不明白这个参数的类型签名是(Bool -> Action)怎样的,如何在第69行它能够使用函数组合运算符<<将Boolfrom targetChecked转换为完整Action类型.
编辑:
这个问题可以简化为"为什么以下工作?"
type Action = Edit Int
do : (Int -> Action) -> Action
do tag = tag(123)
result : Action
result = do(Edit)
Run Code Online (Sandbox Code Playgroud)
定义联合类型时,联合类型的每个标记都将成为已定义的值.所以当你定义:
type Action = Tick | NoOp
Run Code Online (Sandbox Code Playgroud)
这也定义了:
Tick : Action
NoOp : Action
Run Code Online (Sandbox Code Playgroud)
当union标签有参数时,它就变成了"构造函数",一个函数:
type Action = Edit Int
Edit : Int -> Action
Run Code Online (Sandbox Code Playgroud)
(这些标签也可用作可与case- of构造匹配的模式.另请参阅网站上的文档.)