Interpreting elm function declaration syntax -


i not able grasp following function declaration & definition syntax of elm language.

keydown: (int -> msg) -> attribute msg keydown event =    on "keydown" (json.map event keycode) 

what first line means? keydown: (int -> msg) -> attribute msg. means, keydown function takes, int & msg type params & returns of type attribute?

what happens in function definition part?

how parameters function defined?

tl;dr

keydown function accepts function argument , returns attribute, elm's renderer uses attach event listener.

if define type

type msg     = keydown int 

the keydown acts constructor function values of type msg, can implicit type of keydown: int -> msg, want when want retrieve key code dom event.

explanation

first of all, must proper implementation should this:

keydown : (int -> msg) -> attribute msg keydown tagger =     on "keydown" (json.decode.map tagger keycode) 

it important have msg type variable in signature, it's possible use event listener in different parts of application, html emits different types of msg

to understand happening here, let's closer @ html.events.on

on : string -> decoder msg -> attribute msg 

every dom event represented javascript object fields. retrieve data javascript, elm needs pass through decoder ensure type safety in runtime, http requests.

the decoder in on expected produce same type of message, attribute itself.

by default, keycode decoder decodes int value, want emit message.

that's json.decode.map helps int , apply tagger function, produces message.

decoder

the entire (json.map event keycode) part decoder msg, applied event object extract key code , "tag" message.


Comments

Popular posts from this blog

PHP and MySQL WP -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

go - golang pprof for c library code -