Search code examples
scalatypestype-mismatchtraitsmismatch

Scala not resolving X as Y, even though X extends Y


[See snippet Below] I read that I have a X which is a Piece, and I have a Player that requires a Piece. The player Me attempts to define this Piece with Piece X. Yet the Scala does not recognize X as a piece, rather it sees 'X.type'. What does X.type mean? I'm not exactly sure what my problem is so I'm having a hard time searching.

I don't see how this can be type shadowing [ See Scala really weird Type Mismatch ], since I don't specify another 'Piece', rather I only specify that I require a 'Piece'.

It seems like there is something preventing Scala to resolve X as a Piece.

To try in REPL

trait Piece { val piece: Char }
case class X extends Piece { val piece: Char = 'X' }

trait Player { val piece: Piece }
case class Me extends Player { val piece: Piece = X }

Result

error: type mismatch;
 found   : X.type
 required: Piece
       case class Max extends Player { val piece: Piece = X }

Solution

  • That is probably because by X you're only describing a class (so it's actually a type), whereas Scala is expecting an object (or instance) of type Piece. You would get the same error by doing this in the first case:

    case class X extends Piece { val piece: Char = Char }
    

    As you can see, your version works because you pass a specific character, not a type. If you want your code to work, you probably have to create a new object and pass it in:

    case class Me extends Player { val piece: Piece = new X }
    

    EDIT: As the OP noted in the comment below, using X() also works, because as X is a case class it gets an implicit apply() method:

    case class Me extends Player { val piece: Piece = X() }