Scala Adaptor: Inheritance, subscriptions and subjects #490
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.
I tried to add subscriptions and subjects using the value class trick, and came to the conclusion that it won't work. The problem is that inheritance and value classes don't work together, because value classes cannot be extended. We want Observable to be a value class, and at the same time, we want Subject to extend Observable, so that doesn't work.
This PR is very similar to Erik's code, but I added a trait
which all classes extend. This allows us to have an
asJava
method everywhere (instead ofasJavaSubject
/asJavaObserver
etc). The main challenge was to get the double inheritance Subject extends Observer and Observable working.Now all wrappers are done the same way. For instance, Observable looks as follows:
In Scala code, to convert from Scala types to Java types, there's the
asJava
method, and to convert from Java types to Scala types, there's anapply
method in each companion object.When we used value classes, such conversions were not necessary in Java, because Scala types appeared as Java types for the Java compiler. Now, they become necessary, but note that this PR does not yet contain such conversions, but that should be no big problem.