Skip to content

Commit

Permalink
Make all objects Serializable
Browse files Browse the repository at this point in the history
To avoid deadlocks when combining objects, lambdas and multi-threading,
lambdas in objects are compiled to instance methods of the module class
instead of static methods (see tests/run/deadlock.scala and
scala/scala-dev#195 for details).

This has worked well for us so far but this is problematic for
serialization: serializing a lambda requires serializing all the values
it captures, if this lambda is in an object, this means serializing the
enclosing object, which fails if the object does not extend
Serializable.

Because serializing objects is basically free since scala#5775, it seems like
the simplest solution is to simply make all objects Serializable, this
certainly seems preferable to deadlocks.
  • Loading branch information
smarter committed Jan 25, 2019
1 parent 80d9df6 commit aa56079
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions compiler/src/dotty/tools/dotc/ast/Desugar.scala
Original file line number Diff line number Diff line change
Expand Up @@ -338,8 +338,9 @@ object desugar {
case _ => false
}

val isCaseClass = mods.is(Case) && !mods.is(Module)
val isCaseObject = mods.is(Case) && mods.is(Module)
val isObject = mods.is(Module)
val isCaseClass = mods.is(Case) && !isObject
val isCaseObject = mods.is(Case) && isObject
val isImplicit = mods.is(Implicit)
val isEnum = mods.isEnumClass && !mods.is(Module)
def isEnumCase = mods.isEnumCase
Expand Down Expand Up @@ -522,6 +523,8 @@ object desugar {
parents1 = enumClassTypeRef :: Nil
if (isCaseClass | isCaseObject)
parents1 = parents1 :+ scalaDot(str.Product.toTypeName) :+ scalaDot(nme.Serializable.toTypeName)
else if (isObject)
parents1 = parents1 :+ scalaDot(nme.Serializable.toTypeName)
if (isEnum)
parents1 = parents1 :+ ref(defn.EnumType)

Expand Down

0 comments on commit aa56079

Please sign in to comment.