- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Binary compatibility: status and outlook
Fri, 2011-04-29, 18:33
When 2.8 was around the corner there was a big discussion about (the lack of) binary compatibility in Scala.
My answer then was that we would keep binary compatibility for minor revisions and look into ways how to address the problem for major revisions. The first half of the promise was kept. 2.8.1 was binary compatible with 2.8. Now that 2.9 is around the corner, the question is what can we do for binary compatibility now and in the future?
If you are too impatient to read the details: we not there yet, but are making progress. In particular, we have developed the basic technology that lets us maintain binary compatibility for future releases.
Now to the details: There are in fact many sources of binary compatibility. Some of the most common ones are:
1. New methods or fields are added to existing traits. Implementations (but not clients) of these traits need to be recompiled.
For instance, the new collection libraries add methods `par' and `seq' to all collection traits, as well as several other methods.
2. Classes and companion objects are moved to different packages, with type aliases and forwarders in a package object in the old location. Any clients of those classes have to be recompiled.
For instance, the 2.9 standard library has moved several annotation classes from package `scala' to package `scala.annotation'.
3. Lazy vals are added to a class. This shifts the indices
used to access bitmaps in all subclasses.
4. Methods get different signatures. Parameter types might be widened, or result types narrowed or widened. Again, all callers of such a method need to be recompiled.
For instance, collection operations such as `zip' used to take an Iterable as argument, now they take a GenIterable (GenIterable is the common superclass of Iterable and ParIterable). There are many other examples like this.
What can we do to find and fix these? We have been working on a tool which will detect binary compatilities between different versions of a library. The tool is in prototype stage now. We will make it available as soon as it is in a usable state. With the tool, we can have a better idea where binary incompatibilities have been generated, and can roll back if they are accidental.
Some binary incompatibilities are not accidental, but the result of conscious generalizations and enrichments of the libraries. We do not want to sacrifice these possible improvements for binary compatibility. What we do instead is search for technological solutions. For problems of the first three kinds (new trait members, moved classes, new lazy vals), we are working on a tool that can fix these issues by rewriting code on the client side. For problems of the third kind (changed method signatures), we have developed a new solution based on bridge methods. Let's say you have a method
def combine(other: Seq[String]): Unit
and you want to generalize the method to take Traversables instead:
def combine(other: Traversable[String]): Unit
That change is source compatible (all code calling the old `combine' will work with the new one), but it is not binary compatible, because the new method handle has type Traversable in its signature where the old one had Seq. Since callers refer to a method by giving its name and full signature, callers compiled against the old method will not work with the new one.
To fix this kind of problem (which can be diagnosed using our tool), we can add a bridge method:
@bridge def combine(other: Seq[String]): Unit =
combine(other: Traversable[String])
Since there is now a method with the right signature, binary compatibility is maintained. At the same time, the bridge method will be invisible to freshly compiled Scala code, so all recompiled code will pick up only the new method, not the bridge. This also ensures that bridges do not affect overloading or overriding relationships for the other methods; they are purely there for bytecode generation. Using bridge methods, we can compensate the significant changes caused by the arrival of parallel collections in 2.9.
So these are the schemes we are working on. We are reasonably confident that they will solve Scala's binary compatibility once they are ready. For 2.9, bridge methods ensure that quite a lot of code compiled against 2.8 will continue to operate, but my no means all code. So it is very much advised to recompile your projects for 2.9. Recompilation should in most cases be painless because 2.9 is by-and-large source compatible with 2.8. There's one exception: If your application uses features that were already deprecated in 2.8, it might find these features removed in 2.9. So it's a good idea to get rid of deprecation warnings before upgrading.
-- Martin
----------------------------------------------
Martin Odersky
Prof., EPFL and CEO, Scala Solutions
PSED, 1015 Lausanne, Switzerland
My answer then was that we would keep binary compatibility for minor revisions and look into ways how to address the problem for major revisions. The first half of the promise was kept. 2.8.1 was binary compatible with 2.8. Now that 2.9 is around the corner, the question is what can we do for binary compatibility now and in the future?
If you are too impatient to read the details: we not there yet, but are making progress. In particular, we have developed the basic technology that lets us maintain binary compatibility for future releases.
Now to the details: There are in fact many sources of binary compatibility. Some of the most common ones are:
1. New methods or fields are added to existing traits. Implementations (but not clients) of these traits need to be recompiled.
For instance, the new collection libraries add methods `par' and `seq' to all collection traits, as well as several other methods.
2. Classes and companion objects are moved to different packages, with type aliases and forwarders in a package object in the old location. Any clients of those classes have to be recompiled.
For instance, the 2.9 standard library has moved several annotation classes from package `scala' to package `scala.annotation'.
3. Lazy vals are added to a class. This shifts the indices
used to access bitmaps in all subclasses.
4. Methods get different signatures. Parameter types might be widened, or result types narrowed or widened. Again, all callers of such a method need to be recompiled.
For instance, collection operations such as `zip' used to take an Iterable as argument, now they take a GenIterable (GenIterable is the common superclass of Iterable and ParIterable). There are many other examples like this.
What can we do to find and fix these? We have been working on a tool which will detect binary compatilities between different versions of a library. The tool is in prototype stage now. We will make it available as soon as it is in a usable state. With the tool, we can have a better idea where binary incompatibilities have been generated, and can roll back if they are accidental.
Some binary incompatibilities are not accidental, but the result of conscious generalizations and enrichments of the libraries. We do not want to sacrifice these possible improvements for binary compatibility. What we do instead is search for technological solutions. For problems of the first three kinds (new trait members, moved classes, new lazy vals), we are working on a tool that can fix these issues by rewriting code on the client side. For problems of the third kind (changed method signatures), we have developed a new solution based on bridge methods. Let's say you have a method
def combine(other: Seq[String]): Unit
and you want to generalize the method to take Traversables instead:
def combine(other: Traversable[String]): Unit
That change is source compatible (all code calling the old `combine' will work with the new one), but it is not binary compatible, because the new method handle has type Traversable in its signature where the old one had Seq. Since callers refer to a method by giving its name and full signature, callers compiled against the old method will not work with the new one.
To fix this kind of problem (which can be diagnosed using our tool), we can add a bridge method:
@bridge def combine(other: Seq[String]): Unit =
combine(other: Traversable[String])
Since there is now a method with the right signature, binary compatibility is maintained. At the same time, the bridge method will be invisible to freshly compiled Scala code, so all recompiled code will pick up only the new method, not the bridge. This also ensures that bridges do not affect overloading or overriding relationships for the other methods; they are purely there for bytecode generation. Using bridge methods, we can compensate the significant changes caused by the arrival of parallel collections in 2.9.
So these are the schemes we are working on. We are reasonably confident that they will solve Scala's binary compatibility once they are ready. For 2.9, bridge methods ensure that quite a lot of code compiled against 2.8 will continue to operate, but my no means all code. So it is very much advised to recompile your projects for 2.9. Recompilation should in most cases be painless because 2.9 is by-and-large source compatible with 2.8. There's one exception: If your application uses features that were already deprecated in 2.8, it might find these features removed in 2.9. So it's a good idea to get rid of deprecation warnings before upgrading.
-- Martin
----------------------------------------------
Martin Odersky
Prof., EPFL and CEO, Scala Solutions
PSED, 1015 Lausanne, Switzerland
Fri, 2011-04-29, 22:57
#2
Re: Binary compatibility: status and outlook
Hi Martin,
Thanks for the update.
On Fri, Apr 29, 2011 at 6:32 PM, martin odersky wrote:
> We have been working on a tool which
> will detect binary compatilities between different versions of a library.
> The tool is in prototype stage now. We will make it available as soon as it
> is in a usable state. With the tool, we can have a better idea where binary
> incompatibilities have been generated, and can roll back if they are
> accidental.
This is great news.
> We are reasonably confident that they will solve Scala's binary compatibility once they are ready.
As is this.
> So it is very much advised to recompile your projects for 2.9.
Thanks for making this recommendation.
Best,
Ismael
Wed, 2011-05-11, 09:17
#3
Re: Binary compatibility: status and outlook
What's the status of @bridge?
To be clear, it's something that would be used internally by the scala library?
I'm trying to compile reactive-core on RC4 and getting:
[error] /home/naftoli/dev/reactive/reactive-core/src/main/scala/reactive/TransformedSeq.scala:280: method flatMap overrides nothing
[error] override def flatMap[U, That](f: T => Traversable[U])(implicit bf: CanBuildFrom[TransformedSeq[T], U, That]): That =
On Fri, Apr 29, 2011 at 5:45 PM, Ismael Juma <ismael [at] juma [dot] me [dot] uk> wrote:
To be clear, it's something that would be used internally by the scala library?
I'm trying to compile reactive-core on RC4 and getting:
[error] /home/naftoli/dev/reactive/reactive-core/src/main/scala/reactive/TransformedSeq.scala:280: method flatMap overrides nothing
[error] override def flatMap[U, That](f: T => Traversable[U])(implicit bf: CanBuildFrom[TransformedSeq[T], U, That]): That =
On Fri, Apr 29, 2011 at 5:45 PM, Ismael Juma <ismael [at] juma [dot] me [dot] uk> wrote:
Hi Martin,
Thanks for the update.
On Fri, Apr 29, 2011 at 6:32 PM, martin odersky <martin [dot] odersky [at] epfl [dot] ch> wrote:
> We have been working on a tool which
> will detect binary compatilities between different versions of a library.
> The tool is in prototype stage now. We will make it available as soon as it
> is in a usable state. With the tool, we can have a better idea where binary
> incompatibilities have been generated, and can roll back if they are
> accidental.
This is great news.
> We are reasonably confident that they will solve Scala's binary compatibility once they are ready.
As is this.
> So it is very much advised to recompile your projects for 2.9.
Thanks for making this recommendation.
Best,
Ismael
Rewriting code on the client side sounds very intriguing! Is it
possible to find out more details, including the general principle,
and used libraries (if any)?
Can this functionality be used to proxify/specialize/modify classes at runtime?
Is it going to work on Android?
Will there be public API to allow custom code rewriting?
Sorry about asking so many questions. Scala keeps surprising me by
accomplishing seemingly impossible over and over again.