You use
Html.Attribute.key when you have a sequence of items where you may be inserting and removing from the middle. I took this API pretty much directly from the virtual-dom implementation that backs the currently released version of elm-html. I am doing a rewrite for various reasons, and realized that the current API clashes with the laziness optimizations.
Problem
If you have a big list, you’d likely want to make each entry lazy. It would suck to have to build 1000 virtual nodes. If you want keys, you need to add them as an attribute, but lazy takes no attributes! This means you need to add an extra DOM node with a key where it has one child that’s lazy.
Alternative
I am considering getting rid of Html.Attribute.key entirely and replacing it with this function:
lazyDict
: String
-> List Attribute
-> (a -> Html)
-> Dict comparable a
-> Html
lazyDict tagName attributes viewItem items = ...
It is somewhat similar to the normal
Html.node function in that it is just about creating virtual DOM nodes, but in this case you give a
Dict instead of a
List. You might create a news feed like this:
viewNewsFeed : Dict Time Story -> Html
viewNewsFeed stories =
lazyDict "div" [class "news-feed"] viewStory stories
Each entry would also be done lazily, so you only need to create the Html if the dictionary entries are not reference equal.
This means it would be easy to treat the stories as a dictionary in your model. When you want to add something, you add it with a timestamp. If it's sorted by name, you have a (Dict String Person) or whatever.
Question
If you have personally used keys for a specific scenario, would this new API cover things as well? Would it be better or worse? Can you elaborate on the details of your case?
Note: I am looking for specific experience. If you don't have a concrete example, you are off-topic. Take it to another thread. If you are reading this and the thread is long, feel free just to respond to my initial question. The goal here is to gather data, not opinions.