Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Change Node.child and Node.attribute return type to immutable.Seq on 2.13+#760

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
lrytz merged 9 commits intoscala:mainfromlrytz:imm-child
May 27, 2025

Conversation

lrytz
Copy link
Member

@lrytzlrytz commentedMay 19, 2025
edited
Loading

XML values in Scala are immutable

Change the type ofNode.child /Node.nonEmptyChildren andNode.attribute fromcollection.Seq toimmutable.Seq in 2.13+.

A parent is added toNode which defines the old signatures forchild /nonEmptyChildren. This (and the resulting bridge methods) ensures binary compatbility.

@lrytzlrytzforce-pushed theimm-child branch 3 times, most recently from6436666 to08e6c71CompareMay 19, 2025 11:46
@lrytz
Copy link
MemberAuthor

If we go ahead with this PR we'll need a new minor release (2.4.0).

There is more API with return typecollection.Seq that probably always returns animmutable.Seq, e.g.

  • Metadata.value
  • NodeSeq.theSeq
  • MetaData.apply /MetaData.get /Node.attribute
  • TextBuffer.toText
  • BasicTransformer.transform

I'm happy take a look at those as well; I only didchild for now because that's what is affecting the 2.13 migration I'm working on.

Change the type of `Node.child` and `Node.nonEmptyChildren` from`collection.Seq` to `immutable.Seq` in 2.13+.A parent is added to `Node` which defines the old signatures for`child` / `nonEmptyChildren`. This (and the resulting bridge methods)ensures binary compatbility.
@lrytz
Copy link
MemberAuthor

For the record, there is animplicit conversioncollection.Seq[Node] => NodeSeq andNodeSeq is a subclass ofimmutable.Seq on 2.13.

So the following works already without this PR

scala>valn:Node= <a><b/></a>valn: scala.xml.Node= <a><b/></a>scala>valc: collection.immutable.Seq[Node]= n.childvalc:Seq[scala.xml.Node]=Seq(<b/>)

But this fails:

scala>valc: collection.immutable.Seq[String]= n.child.map(_.toString)^error:typemismatch;found   :Seq[String] (in scala.collection)required:Seq[String] (in scala.collection.immutable)

@retronymretronym self-requested a reviewMay 19, 2025 14:10
@lrytz
Copy link
MemberAuthor

probably always returns animmutable.Seq

I took a closer look.

MarkupParser passes aNodeBuffer – I'll change that.

The compiler uses aNodeBuffer for XML literals and passes it directly toElem($buf). That worked on 2.12, sochild is actually a mutable collection. On 2.13 the implicitseqToNodeSeq makes it compile. Details here:scala/scala#11065

@lrytzlrytzforce-pushed theimm-child branch 4 times, most recently from68e70b7 to80c3a0dCompareMay 21, 2025 08:48
@lrytzlrytz changed the titleChange Node.child return type to immutable.Seq on 2.13+Change Node.child and Node.attribute return type to immutable.Seq on 2.13+May 21, 2025
@lrytzlrytzforce-pushed theimm-child branch 2 times, most recently from8631ddb toc60701eCompareMay 21, 2025 11:29
@lrytz
Copy link
MemberAuthor

I also changedNode.attribute and the relatedMetaData.apply /MetaData.value /MetaData.get methods to returnimmutable.Seq.

And I changedNodeSeq.theSeq, even though this method is probably not used often as part of the public API. The reason is thatNodeSeq extendsimmutable.Seq, and iftheSeq is not immutable, it is essentially an unsafe immutable wrapper around a generic collection:

scala>valb=newNodeBuffer()+= <b/>scala>valx:NodeSeq=Elem(null,"a",Null,TopScope,true,NodeSeq.fromSeq(b):_*)valx: scala.xml.NodeSeq= <a><b/></a>scala> b+= <c/>scala> xvalres0: scala.xml.NodeSeq= <a><b/><c/></a>

I didn't changeBasicTransformer.transform because that API is expected to be subclassed, changes would likely cause source incompatibilities.

@lrytz
Copy link
MemberAuthor

lrytz commentedMay 21, 2025
edited
Loading

This is ready, if you'd like to take a look@dubinsky.

Besides mima, I also usedjardiff tocompare the changes and it looks as expected.

@retronym
Copy link
Member

retronym commentedMay 23, 2025
edited by lrytz
Loading

One minor comment -- it might be more efficient to useVectorBuilder rather thanNodeBuffer (which extendsArrayBuffer adding the utility method&+).

@lrytz
Copy link
MemberAuthor

Right.. We have to keepNodeBuffer extends ArrayBuffer though, because of source and binary compatibility. So the code emitted by the compiler for literals will keep usingArrayBuffer.

We could use aVectorBuilder within scala-xml on 2.13, but it would involve changing public signatures, e.g.MarkupParser.content1(NamespaceBinding, NodeBuffer): Unit.

So it's probably not worth the effort...

@retronym
Copy link
Member

Agreed. If we were maximising performance from XML literals we'd be better to use an immutable ArraySeq builder with a size hint as the compiler knows how many elements there are. But maximising performance isn't really a design goal of scala-xml IMO, so the small overhead ofArrayBuffer =>Vector is okay.

@lrytzlrytz merged commitc2c76c8 intoscala:mainMay 27, 2025
31 checks passed
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@retronymretronymretronym approved these changes

@dubinskydubinskydubinsky approved these changes

Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

3 participants
@lrytz@retronym@dubinsky

[8]ページ先頭

©2009-2025 Movatter.jp