Skip to content

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

Closed
@scabug

Description

@scabug

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions