Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Companion object of Serializable type becomes serializable too #9668

Closed
scabug opened this issue Feb 22, 2016 · 3 comments
Closed

Companion object of Serializable type becomes serializable too #9668

scabug opened this issue Feb 22, 2016 · 3 comments

Comments

@scabug
Copy link

scabug commented Feb 22, 2016

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)

@scabug
Copy link
Author

scabug commented Feb 22, 2016

Imported From: https://issues.scala-lang.org/browse/SI-9668?orig=1
Reporter: @sjrd
Affected Versions: 2.11.2, 2.11.7

@scabug scabug added this to the Backlog milestone Apr 7, 2017
hrhino added a commit to hrhino/scala that referenced this issue Jan 1, 2020
I nearly resolved to clear the scala/bug backlog this year.

Fixes scala/bug#3899
Fixes scala/bug#7686
Fixes scala/bug#9668
@som-snytt
Copy link

I see the linked PR was demonstrating that the example:

scalac t9668.scala && scala Test
t9668.scala:16: error: type mismatch;
 found   : testing.Foo.type
 required: Serializable
    println(testing.Foo: Serializable)
                    ^
one error found

because type aliases don't have companions.

But true companions are still made serializable.

@som-snytt
Copy link

scala/scala#5602 includes discussion confirming that the type alias fix was intended.

@SethTisue SethTisue removed this from the Backlog milestone Jan 28, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants