com-lihaoyi / scalatags

ScalaTags is a small XML/HTML construction library for Scala.
https://com-lihaoyi.github.io/scalatags/
MIT License
758 stars 117 forks source link

Request: Add support for maps/iterables in tags #132

Closed danielyli closed 8 years ago

danielyli commented 8 years ago

For-expressions involving Seqs and Lists currently work within tags, but Maps (or, more specifically, Iterables) do not.

For example, the following compiles:

div(
  for {
    (x, y) <- Seq(("one", 1), ("two", 2))
  } yield div(x, y)
)

But this does not:

div(
  for {
    (x, y) <- Map("one" -> 1, "two" -> 2)
  } yield div(x, y)
)
[error] type mismatch;
[error]  found   : scala.collection.immutable.Iterable[scalatags.JsDom.TypedTag[org.scalajs.dom.html.Div]]
[error]     (which expands to)  scala.collection.immutable.Iterable[scalatags.JsDom.TypedTag[org.scalajs.dom.raw.HTMLDivElement]]
[error]  required: scalatags.JsDom.Modifier
[error]     (which expands to)  scalatags.generic.Modifier[org.scalajs.dom.raw.Element]
[error]             (x, y) <- Map("one" -> 1, "two" -> 2)
[error]                    ^
[error] one error found

Changing the map to a sequence with .toSeq fixes the issue.

This seems to be unintuitive and surprising behavior, since the tags effectively can handle Seqs and Lists but not Iterables. Is it possible to add support?

lihaoyi commented 8 years ago

Probably not gonna happen. The thing about maps and sets is that they are fundamentally unstructured: your map is going to render differently each time. If you want that, you can call .toSeq, but I don't think it should be the default