Actually, the situation is more subtle.

On the one hand, the two definitions

onClick : String -> Bool -> (State -> msg) -> Attribute msgonClick
name isReversed toMsg =
  Html.Events.on "click" <| Json.map toMsg <|
    Json.object2 State (Json.succeed name) (Json.succeed isReversed)

and

onClick : String -> Bool -> (State -> msg) -> Attribute msgonClick
name isReversed toMsg =
   Html.Events.onClick (toMsg (State name isReversed))

are indeed semantically equivalent.

But on the other hand, if one has two independent calls onClick name
isReversed toMsg and onClick name isReversed toMsg with the same name,
isReversed and toMsg, then for the first, more complicated, definition of
onClick, this native code in the core package
<https://github.com/elm-lang/core/blob/c1d0e4d19639649dafd63daf320890cfb6ef0c99/src/Native/Json.js#L520-L591>
will be able to determine at runtime that the two mentioned independent
calls actually describe the same event handler, whereas for the second
definition of onClick that code will not be able to determine that. And *that
difference* (though not semantically visible) has an impact in virtual dom
diffing, and thus on the performance of the overall program.

See also this comment by Evan
<https://github.com/evancz/elm-sortable-table/pull/5#issuecomment-243187276>
.
​

2016-08-21 17:27 GMT+02:00 Janis Voigtländer <janis.voigtlaen...@gmail.com>:

> You are correct.
>
> The definition
>
> onClick : String -> Bool -> (State -> msg) -> Attribute msgonClick name 
> isReversed toMsg =
>   E.on "click" <| Json.map toMsg <|
>     Json.object2 State (Json.succeed name) (Json.succeed isReversed)
>
> is indeed equivalent (by simple laws about map, object2, and succeed) to:
>
> onClick : String -> Bool -> (State -> msg) -> Attribute msgonClick name 
> isReversed toMsg =
>   E.on "click" <| Json.succeed <|
>     toMsg (State name isReversed)
>
> and given the definition of E.onClick in elm-lang/html that is equivalent
> to:
>
> onClick : String -> Bool -> (State -> msg) -> Attribute msgonClick name 
> isReversed toMsg =
>   E.onClick <|
>     toMsg (State name isReversed)
>
> ​
>
> 2016-08-21 17:18 GMT+02:00 Bernardo <auxs...@gmail.com>:
>
>> Hi,
>>
>> I'm trying to follow the example of elm-sortable-table https://git
>> hub.com/evancz/elm-sortable-table/blob/master/src/Table.elm.
>>
>> I'd like if someone can explain the definition of the onClick function.
>>
>> onClick : String -> Bool -> (State -> msg) -> Attribute msg
>> onClick name isReversed toMsg =
>>   E.on "click" <| Json.map toMsg <|
>>     Json.object2 State (Json.succeed name) (Json.succeed isReversed)
>>
>>
>> As a newbie this is what I'd write.
>>
>> onClick : String -> Bool -> (State -> msg) -> Attribute msg
>> onClick name isReversed toMsg =
>>   E.onClick (toMsg (State name isReversed))
>>
>> It type checks... but I guess it must be something different.
>>
>> Thank you.
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Elm Discuss" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to elm-discuss+unsubscr...@googlegroups.com.
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>

-- 
You received this message because you are subscribed to the Google Groups "Elm 
Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to elm-discuss+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to