Skip to content

difference between "object A" and A.type #3083

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

Closed
scabug opened this issue Feb 20, 2010 · 2 comments
Closed

difference between "object A" and A.type #3083

scabug opened this issue Feb 20, 2010 · 2 comments

Comments

@scabug
Copy link

scabug commented Feb 20, 2010

The problem exists in both 2.7 and 2.8. When I try to compile this code:

object Test extends Application {
  def x: A.type = A
  def y: B[A.type] = new B(A)
}

object A

class B[N](n: N)

, I have no problem with defining x. For the definition of y however I get:

Test.scala:3: error: type mismatch;
 found   : B[object A]
 required: B[A.type]

If I look at the compiler's typer phase, the two definitions are as follows:

scalac -Xprint:typer Test.scala

    def x: object A = A;
    def y: B[A.type] = new B[object A](A)

We see in the first definition that "A.type" is simply translated into "object A" : the two expressions seem to mean the same thing. However in the second definition they obviously do not, as they are causing a type mismatch.

I can compile the definition of y if I force the type parameter to A.type:

  def y: B[A.type] = new B[A.type](A)

My question : do "object A" and A.type mean the same thing, or not ? If not, why does the definition of x work ? If they do, why does the definition of y fail ? Thanks

@scabug
Copy link
Author

scabug commented Feb 20, 2010

Imported From: https://issues.scala-lang.org/browse/SI-3083?orig=1
Reporter: @rjolly

@scabug
Copy link
Author

scabug commented Feb 20, 2010

@paulp said:
This is a duplicate of perennial favorite #1208. If you want more explanation than you can find in that ticket (and others) please ask on one of the mailing lists.

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

1 participant