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

Possible Regression: Defining type covariant type alias fails when using in invariant position same compilation unit #10140

Closed
scabug opened this issue Jan 10, 2017 · 6 comments
Assignees

Comments

@scabug
Copy link

scabug commented Jan 10, 2017

This code used to work in Scala-2.11

type Id[+A] = A

trait Meh[F[_]] { def foo[A](a: => A): F[A] }

val idmeh = new Meh[Id] { 
    def foo[A](a: => A): Id[A] = a 
}

Now this fails with

<pastie>:18: error: covariant type Id occurs in invariant position in type => Meh[Id] of value idmeh
       val idmeh = new Meh[Id] {

However, If I change where I define type Id[+A] = A then it works.

scala> :paste
// Entering paste mode (ctrl-D to finish)

type Id[+A] = A

trait Meh[F[_]] { def foo[A](a: => A): F[A] }

// Exiting paste mode, now interpreting.

defined type alias Id
defined trait Meh

scala> val idmeh = new Meh[Id] {
     |     def foo[A](a: => A): Id[A] = a
     | }
idmeh: Meh[Id] = $anon$1@77fdbd9b

I also get this behaviour with real source files, not just in the repl.

@scabug
Copy link
Author

scabug commented Jan 10, 2017

Imported From: https://issues.scala-lang.org/browse/SI-10140?orig=1
Reporter: @hamnis
Affected Versions: 2.12.1
See #8079

@scabug
Copy link
Author

scabug commented Feb 14, 2017

David Koeplinger (dkoeplin) said (edited on Feb 14, 2017 10:26:03 AM UTC):
Just ran into this as well. Another example:

trait X {
  type Id[+A] = A
  def foo[A](x: Id[A]) = x
}

error: covariant type Id occurs in contravariant position in type X.this.Id[A] of value x

However, if the type alias is defined in a separate trait, there's no error.

trait X { 
  type Id[+A] = A 
}
trait Y extends X {
  def foo[A](x: Id[A]) = x
}

@scabug
Copy link
Author

scabug commented Feb 15, 2017

@SethTisue said:
perhaps an effect of the fix for SI-8079?

@scabug
Copy link
Author

scabug commented Feb 15, 2017

@SethTisue said:
@retronym this might interest you

@benjaminfrank
Copy link

We have com about this recently as well, here is what we did:

// Does compile using Scala 2.11 (REPL)
// Does not compile using Scala 2.12 (REPL)
// Compiles using dotty (REPL)
trait TC[F[_]]; type F[+T] = T; new TC[F] {}

// Workaround for Scala 2.12 (REPL)
trait TC[F[_]]; object Foo {type F[+T] = T}; new TC[Foo.F] {}

@adriaanm adriaanm modified the milestones: 2.12.4, 2.12.5 Oct 10, 2017
@SethTisue SethTisue modified the milestones: 2.12.5, Backlog Mar 1, 2018
@dwijnand
Copy link
Member

No longer the case

$ m test.scala
object Test {
  type Id[+A] = A

  trait Meh[F[_]] { def foo[A](a: => A): F[A] }

  val idmeh = new Meh[Id] {
      def foo[A](a: => A): Id[A] = a
  }
}
$ scalac -version
Scala compiler version 2.13.5 -- Copyright 2002-2020, LAMP/EPFL and Lightbend, Inc.
$ scalac test.scala
$

@dwijnand dwijnand removed this from the Backlog milestone Apr 30, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants