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

unchecked variance declarations in type lambdas as method type parameters #7872

Closed
scabug opened this issue Sep 24, 2013 · 3 comments
Closed

Comments

@scabug
Copy link

scabug commented Sep 24, 2013

Seems vaguely related to #1364.

oops1 & oops2 below should not compile; they throw as commented at runtime.

This seems specific to the extra structural type & projection; see how up[Stringer] fails to compile, but a trivial wrapper around Stringer causes it to pass.

// coinv.scala
object coinv {
  def up[F[+_]](fa: F[String]): F[Object] = fa
  def down[F[-_]](fa: F[Object]): F[String] = fa

  up(List("hi"))
  // [error] type A is covariant, but type _ is declared contravariant
  // down(List('whatever: Object))

  // should not compile; `l' is unsound
  def oops1 = down[({type l[-a] = List[a]})#l](List('whatever: Object)).head + "oops"
  // scala> oops1
  // java.lang.ClassCastException: scala.Symbol cannot be cast to java.lang.String
  //         at com.nocandysw.coinv$.oops1(coinv.scala:12)

  type Stringer[-A] = A => String
  down[Stringer](_.toString)
  // [error] type A is contravariant, but type _ is declared covariant
  // up[Stringer]("printed: " + _)

  // should not compile; `l' is unsound
  def oops2 = up[({type l[+a] = Stringer[a]})#l]("printed: " + _)
  // scala> oops2(Some(33))
  // java.lang.ClassCastException: scala.Some cannot be cast to java.lang.String
  //         at com.nocandysw.coinv$$anonfun$oops2$1.apply(coinv.scala:20)
}
@scabug
Copy link
Author

scabug commented Sep 24, 2013

Imported From: https://issues.scala-lang.org/browse/SI-7872?orig=1
Reporter: Stephen Compall (s11001001)
Affected Versions: 2.10.3-RC3, 2.11.0-M5

@scabug
Copy link
Author

scabug commented Sep 24, 2013

Stephen Compall (s11001001) said:
Similarly for invariant positions:

def oops3 = up[({type l[+a] = a => a})#l](_ + "hi")('fortytwo)
// java.lang.ClassCastException: scala.Symbol cannot be cast to java.lang.String

@scabug
Copy link
Author

scabug commented Nov 22, 2013

@retronym said:
scala/scala#3178

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