Skip to content

Companion object of Serializable type becomes serializable too #9668

@scabug

Description

@scabug

File Test.scala:

package bug

package object testing {
  type Foo = String
  // alternatives:
  // trait Foo extends Serializable
  // type Foo <: String

  object Foo
}

object Test {
  def main(args: Array[String]): Unit = {
    // This should not display scala.Serializable
    println(testing.Foo.getClass.getInterfaces().mkString(", "))

    // This should not even compile:
    println(testing.Foo: Serializable)
  }
}

Compile and run:

doeraene@lamppc37:~/projects/temp/scalac-bug-serializable-companion$ ~/opt/scala-2.11.2/bin/scalac Test.scala
doeraene@lamppc37:~/projects/temp/scalac-bug-serializable-companion$ ~/opt/scala-2.11.2/bin/scala -cp . bug.Test
interface scala.Serializable

This can be particularly annoying in Scala.js, because it makes it impossible to declare a type alias whose companion is a JavaScript type (since JavaScript types cannot extend non-JS types such as Serializable). See scala-js/scala-js#2244 (comment)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions