Scala developer's dependency on Java

Scala is a great language. One of its strengths is the effortless interoperability with Java. However, given the fact that the language is still evolving, some compromises are needed in order to facilitate cooperation between Scala in Java. In effect, sometimes you can't write 100% pure Scala solutions. Sometimes you have to add some Java code to your project, even if very simple one. There are two common examples of this: enumerations and annotations.

Enumerations

Both Java and Scala support enumerations. While the first has a special language feature reserved for them, the later uses the already powerful type system to emulate them convincingly enough. Unfortunately, Scala's enumerations are not convincing enough when one wants to use them in Java-oriented code. One scenario could be using ORMLite library, which supports Java enumerations, but knows nothing about Scala's. Unfortunately, there is no way to create a Java-compatible enumeration in pure Scala. On the other hand, nothing stops you from creating a pure Java one and then using it in Scala - that works just fine! So, 99.99% of your code base could still be Java, with an occasional Java enum here and there. Small code example:

  1. Enums.java:
  2.  
  3. public class Enums {
  4. 	RecordType {
  5. 		New,
  6. 		Updated,
  7. 		Deleted
  8. 	}
  9. }
  10.  
  11. Code.scala
  12.  
  13. import Enums
  14.  
  15. object Code {
  16. 	def test(rt: Enums.RecordType) = rt match {
  17. 		case Enums.RecordType.New => "New record"
  18. 		case Enums.RecordType.Updated => "Updated record"
  19. 		case Enums.RecordType.Deleted => "Deleted record"
  20. 	}
  21. }

Annotations

Annotations can be used to provide metadata about classes and their members. This metadata can be queried during runtime using reflection. Both Scala and Java provide their own annotations. While Scala also provides its own reflection, the API is in active development and has been changing between recent Scala releases, in parallel with the language macros' development (see this recent post for some interesting details - things will change in Scala 2.12 yet again). This makes it not very suitable for long-term usage, especially when you want to achieve something relatively simple. Furthermore, Scala's own StaticAnnotation doesn't derive from Java's Annotation, which makes it inaccessible for runtime inspection. Therefore, one has to write an annotation in Java. Then it is easy to do in Scala something as simple as this:

  1. obj.getClass.getFields.filter(x => x.isAnnotationPresent(classOf[MyJavaAnnotation]))

Conclusion

From these two examples we can clearly see, that it is still important for a good Scala developer to know and understand Java. Going forward Scala will improve and less Java code will need to be written in order to incorporate existing libraries into your projects, however the sheer amount of existing and often useful Java code is unlikely to make the language obsolete. Therefore, Scala and Java will remain road companions for years to come, no matter whether some people like it or not.

Add new comment

Subscribe to Comments for "Scala developer's dependency on Java"