I did a lightning talk last night on how Scala generates valid byte code and some implications for interoperating with Java, including one new issue I discovered with using Function traits in Scala v2.8.0. Nils said he has also seen the problem.

The details are in the presentation notes on GitHub. The essential idea is this. In v2.7.7, if you wanted to instantiate a Function trait in Java code, you would write something like this, for example:

  
  Function1<String,String> toUpper = 
    new Function1<String,String>() {
      public String apply(String s) {
        return s.toUpperCase();
      }
      public int $tag() { return 0; }
    };
  

The $tag method is now gone, but this code won’t work anymore, apparently because of the new @specialized annotation. When you compile this code, you’ll get an error that the abstract andThen[Double,String] method is undefined. I believe this is because specialized versions of the function are instantiated for the AnyVal types.

The workaround is to declare a subclass of Function[String,String] in Scala code with a default apply method implementation. Let’s call it StringToString. A good default implementation is the Template Method Pattern; have the method call an abstract method you declare, say doApply(String): String. Then, back in your Java code, subclass StringToString and implement doApply.

Sorry, comments are closed for this article.