这是取自elm复选框examples -
的视图片段view address model =
div [] <|
span [toStyle model] [text "Hello, how are yo?"]
:: br [] []
:: checkbox address model.red Red "red"
++ checkbox address model.underline Underline "underline"
++ checkbox address model.bold Bold "bold"
checkbox : Address Action -> Bool -> (Bool -> Action) -> String -> List Html
checkbox address isChecked tag name =
[ input
[ type' "checkbox"
, checked isChecked
, on "change" targetChecked (Signal.message address << tag)
]
[]
, text name
, br [] []
]
1)据我所知,double colons
和double plus
用于连接列表?它们彼此有什么不同?
2)在此行的checkbox
函数(Signal.message address << tag)
中,tag
取消绑定的内容是什么?是Red
(或)red
吗?这个论点用于什么?
3)address
函数的参数类型是什么?
答案 0 :(得分:2)
(::)
adds a single element to the start of a list.(++)
concatenates two lists. tag
would be equal to Red
in the when checkbox
is called from checkbox address model.red Red "red"
. Red
is a function from Bool
to Action
. It wraps the event of the check box in this data constructor so that later in the update
function, you can distinguish the event of that check box from the events of the other check boxes.address
is not a function. It has type Address Action
, meaning it holds the address of a mailbox that can receive Action
messages. This "mailbox" is not visible in the code, it's used internally by StartApp
, which is used in the main
function. To keep this question useful even if the linked example changes, these are the relevant code portions I'm referring to:
The update
function:
update action model =
case action of
Red bool ->
{ model | red <- bool }
Underline bool ->
{ model | underline <- bool }
Bold bool ->
{ model | bold <- bool }
The Action
type:
type Action
= Red Bool
| Underline Bool
| Bold Bool
The main
function:
main =
StartApp.start { model = initialModel, view = view, update = update }