scalascala.jsscalajs-react

How to understand this code line?


What is this code line doing? Is this syntactic sugar for another notation?

def createItem(itemText: String) = <.li(itemText)
<.ul(props map createItem: _*)                       <-- this one

Solution

  • I assume that you find the line too strange from a purely syntactical point of view.

    The < is a member of another strangely named entity html_<^.

    The < gizmo is of type HtmlTags, and in particular it has methods li and ul, that correspond to tags <li> and <ul>.

    Therefore <.ul(foobar) is a method call on < of the method ul with arguments foobar.

    The foo: _* syntax is for passing collections to vararg methods.

    To summarize:

    So, essentially, it just constructs an unordered list of some sort.

    Here is a brief explanation from the project github page about the naming of these methods:

    Tags and tag attributes are namespaced; tags under < (because <.div looks similar to ), and attributes under ^ (because something concise was needed and you usually have many attributes which written on new lines all looks to point up back to the target tag).