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

covariant value classes currently impossible #6034

Closed
scabug opened this issue Jul 5, 2012 · 3 comments
Closed

covariant value classes currently impossible #6034

scabug opened this issue Jul 5, 2012 · 3 comments
Assignees
Milestone

Comments

@scabug
Copy link

scabug commented Jul 5, 2012

Even the simplest use of covariance will not compile:

final class OptPlus[+A](val x: A) extends AnyVal { }

// ./a.scala:1: error: covariant type A occurs in contravariant position in type OptPlus[A] of value $this
// final class OptPlus[+A](val x: A) extends AnyVal { }
//             ^
// one error found

The reason for this error (and there are additional ones from completely synthetic methods if one declares a case class, but they can be addressed the same way) is the direct exposure of the type parameter in incoming position of extension methods:

object OptPlus {
  final <synthetic> def extension$hashCode[A >: Nothing <: Any]($this: OptPlus[A])(): Int = $this.x.hashCode();
}

The extension methods should be generated with a bound:

  // Not like this
  def f[A]($this: OptPlus[A])(args) = ...
  
  // Like this instead
  def f[A1 >: A]($this: OptPlus[A1])(args) = ...
  
  // In the case of hashCode and others which don't reference the
  // type parameter at all, they could be like this
  def f($this: OptPlus[_])(args) = ...
  // but I don't know if there's any meaningful advantage to that.
@scabug
Copy link
Author

scabug commented Jul 5, 2012

Imported From: https://issues.scala-lang.org/browse/SI-6034?orig=1
Reporter: @paulp
Other Milestones: 2.10.0

@scabug
Copy link
Author

scabug commented Sep 1, 2012

@paulp said:
Misdiagnosed; fixed in paulp/scala@34893e5d77 .

@scabug scabug closed this as completed Sep 1, 2012
@scabug
Copy link
Author

scabug commented Oct 3, 2012

@adriaanm said:
scala/scala#1227

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

2 participants