Content-Length: 277437 | pFad | http://github.com/scala/scala/pull/11065

E5 Use `toVector` for XML literal sequences by lrytz · Pull Request #11065 · scala/scala · GitHub
Skip to content

Use toVector for XML literal sequences #11065

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: 2.13.x
Choose a base branch
from
Open

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

2 participants








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/scala/scala/pull/11065

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy