Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

lrytz
Copy link
Member

@lrytz lrytz commented May 20, 2025

In <a><b/><c/><a>, a NodeBuffer (which extends ArrayBuffer) is used to accumulate the children. The buffer is passed to new Elem($buf: _*), which only works thanks to the implicit collection.Seq[Node] => NodeSeq declared in scala-xml.

With -Vprint:typer:

scala> <a><b/></a>
[[syntax trees at end of                     typer]] // <console>

      private[this] val res0: scala.xml.Elem = new scala.xml.Elem(null, "a", scala.xml.Null, scala.xml.TopScope, false, (xml.this.NodeSeq.seqToNodeSeq({
        val $buf: scala.xml.NodeBuffer = new scala.xml.NodeBuffer();
        $buf.&+(new scala.xml.Elem(null, "b", scala.xml.Null, scala.xml.TopScope, true));
        $buf
      }): _*));

The implicit was not inserted in 2.12 because the varargs parameter of Elem accepted a collection.Seq.

@scala-jenkins scala-jenkins added this to the 2.13.17 milestone May 20, 2025
@lrytz
Copy link
Member Author

lrytz commented May 20, 2025

I went for Vector because it's more compact than List, and it's more efficient than List or ArraySeq for concatenation / prepending / appending. NodeSeq.newBuilder uses a ListBuffer though..

@lrytz
Copy link
Member Author

lrytz commented May 20, 2025

... this affects IntelliJ not anymore, updated the PR so that plain XML sequences keep type NodeBuffer.

image

In `<a><b/><c/><a>`, a `NodeBuffer` (which extends `ArrayBuffer`) is
used to accumulate the children. The buffer is passed to
`new Elem($buf: _*)`, which only works thanks to the implicit
`collection.Seq[Node] => NodeSeq` declared in scala-xml.

With `-Vprint:typer`:

```scala
scala> <a><b/></a>
[[syntax trees at end of                     typer]] // <console>

      private[this] val res0: scala.xml.Elem = new scala.xml.Elem(null, "a", scala.xml.Null, scala.xml.TopScope, false, (xml.this.NodeSeq.seqToNodeSeq({
        val $buf: scala.xml.NodeBuffer = new scala.xml.NodeBuffer();
        $buf.&+(new scala.xml.Elem(null, "b", scala.xml.Null, scala.xml.TopScope, true));
        $buf
      }): _*));
```

The implicit was not inserted in 2.12 because the varargs parameter of
Elem accepted a `collection.Seq`.
@SethTisue
Copy link
Member

eligible for forward-port to Scala 3?

@lrytz
Copy link
Member Author

lrytz commented Aug 8, 2025

That's already done: scala/scala3#23221

@SethTisue SethTisue merged commit 177b745 into scala:2.13.x Aug 8, 2025
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants