Skip to content
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

Port all Scala code to Java #4030

Open
7 tasks
Scott-Guest opened this issue Feb 22, 2024 · 3 comments
Open
7 tasks

Port all Scala code to Java #4030

Scott-Guest opened this issue Feb 22, 2024 · 3 comments
Assignees
Labels

Comments

@Scott-Guest
Copy link
Contributor

Scott-Guest commented Feb 22, 2024

While Scala is a nice language, the consensus of the frontend team is that maintaining a mixture of Scala and Java causes more friction than it is worth:

  • IntelliJ struggles to reason about the Scala code and reports false errors (Address all errors reported by IntelliJ #3867)
  • We have to frequently convert between Java and Scala types (see the usages of collections.scala) and write boilerplate code to offer two versions of the same methods (e.g. getOption and getOptional in Att.scala).
  • As a company, my impression is that more of us are familiar with the intricacies of Java than of Scala.

As a result, we have decided that

  • All new code should be written in Java
  • In the longterm, all existing Scala code should be ported to Java as well

The largest blocker will be the matching library in the LLVM backend, which makes heavy usage of pattern matching and likely can't be ported until we update to Java 21 (#3499).

However, searching the frontend, there are only 47 Scala files, and many of them can be straightforwardly ported even without pattern matching support in Java.

A high level roadmap:

  • Port Scala case classes not involved in pattern matching to Java record classes inheriting from a sealed interface
  • Port Scala classes/algorithms not making use of pattern matching
  • Make remaining Scala classes as Java-esque as possible
    • Port any trait to an interface
    • Use Java types for all members that are not pattern matched on
      • We already have pcollections as a dependency, which provides immutable functional data structures
  • Update to Java 21
  • Port remaining Scala code
@Scott-Guest
Copy link
Contributor Author

Scott-Guest commented Feb 22, 2024

Another option might be to extend @Baltoli's pattern matching library then port matching to C++.

This would be more work in the short term, but would also make the LLVM backend more cohesive and get rid of the reverse dependency on K.

@ehildenb
Copy link
Member

Love the idea of removing the reverse dependency! LLVM backend being self-contained seems worth the effort. That matching code is just used for LLVM codegen right?

@Baltoli
Copy link
Collaborator

Baltoli commented Feb 23, 2024

Yep, that's the only place it's used. I think we'd want to first build a C++ implementation that generates the same YAML as the scala code does, test equivalence of the outputs, then eliminate the intermediate YAML. It would be a reasonably big rewrite but Dwight's documentation is really good, and we have a strong way of testing any new implementation!

rv-jenkins pushed a commit that referenced this issue Mar 18, 2024
Part of #4030.

Port the `POSet` class from Scala to Java. 

This is mostly a straightforward conversion from Scala functional idioms
(`map`, `filter`, `reduce`, etc.) to the corresponding Java Stream
methods. To port `lazy val`s, we also implement a `Lazy<T>` wrapper
which caches the result of a `Supplier<T>`.

The only remaining reference to Scala here is a single constructor which
takes a `scala.collection.Set<Tuple2<T, T>>` and internally converts it
to a `java.util.Set<Pair<T, T>>`. This can be removed once we more
pervasively switch away from Scala collection types everywhere in the
codebase.

---------

Co-authored-by: Bruce Collie <brucecollie82@gmail.com>
rv-jenkins pushed a commit that referenced this issue Mar 19, 2024
Part of #4030

Ports the `TopologicalSort` class to Java. 

Once again, this is a direct translation, replacing functional idioms
with either direct iteration or equivalent `Stream` functions. The only
semantic change is returning an `Optional` rather than reporting an
error when there's a cycle.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants