Repository navigation
generated domain classes: add unapply to named tuples for easy pattern matching - #366
Merged
Merged
Conversation
ml86
approved these changes
Sep 14, 2026
ml86
left a comment
Contributor
There was a problem hiding this comment.
From my point of view we could drop Scala 2 support. This would also make our life easier to get a Bazel build for this repo going. Scala 3 is 5 years old by now. I dont think somebody could complain about not having enough time to switch and even if so Scala 3 code is supposedly callable from Scala 2 code...
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
the new test file demonstrates how this
unapplymethod returning a named tuple enables pattern matching on the domain classes with the familiar syntax from matching on case classes:Now, we have a DSL that for filtering and extracting values from iterators. I don't aim to replace it here, but to enhance it. I think this feature here will shine in e.g. places (e.g. lowering tests) where you want to check a whole lot of values of the same node at once, and when you're using pattern matching already to replace an
ifon some match arms with a pattern (i.e. turncase x: X if x.foo == ...intocase X(foo = ...).This PR is made a bit awkward because named tuples are a Scala 3 feature and flatgraph still supports Scala 2. My strategy here was to make this feature optional: by default the generated code works with both Scala 2 and 3. But the code generator can be told to to use Scala 3 features, and then you get these named tuples. (You could even run the generator for the same schema twice, once to put code into
src/main/scala2and once intosrc/main/scala3if you wanted a project to be compatible with either version and still make use of this feature in code that's specific to Scala 3.)The interesting diff of this PR is in
domain-classes-generator/src/main/scala/flatgraph/codegen/DomainClassesGenerator.scalaandtests-scala3/src/test/scala/flatgraph/traversal/NamedTupleUnapplyTests.scalaWe could also completely drop Scala 2 - we don't use it anymore in joern. But I'd feel a little bad doing that just for this feature.