1

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
user1091344
  • 612
  • 6
  • 27

1 Answers1

3

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:

  • props is some collection that is
  • mapped using a function createItem and the result is then
  • passed as vararg to method ul of
  • the HtmlType-typed member < of
  • the object / package html_<^

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).

Andrey Tyukin
  • 43,673
  • 4
  • 57
  • 93
  • Thanks. Because of you, I found the correct places for the parenthesis. `<.ul(props.map(createItem): _*)` Now it is easier for me to understand it :) – user1091344 Mar 13 '18 at 18:24
  • @user1091344 Ah, that's another kind of syntactic sugar. You can usually write `foo.bar(baz)` in infix notation `foo bar baz`. – Andrey Tyukin Mar 13 '18 at 18:38