Type mismatch, expected: T, actual: T

EthanP

I'm not understanding what this error wants me to do:

Type mismatch, expected: T, actual: T

I only have 3 lines of code:

case class BaseElem[T](e: T)
case class OrderedElem[T <: Ordered](override val e: T) extends BaseElem[T](e) with Ordered[OrderedElem[T]] {
  override def compare(that: OrderedElem[T]): Int = this.e.compare(that.e)
}

BaseElem is a simple container of T's. OrderedElem is an Ordered container of T <: Ordered. So I want a comparison between OrderedElems to be the comparison of their respective elements.

The error is in the override of compare, the code highlighted by the error is that.e.

  1. What is this error saying and how do I fix it?

  2. Side question, can the declaration of OrderedElem be simplified and retain the desired semantics?

EthanP

The issue was with the part OrderedElem[T <: Ordered]. After following om-nom-nom's smell cleanups and fixing the error he noted, I found that types like Int (with primitive representations) don't extent Ordered in Scala (see this question), so one must use a "view bound" <% to tell Scala to also look for available implicit conversions.

Now I have:

class BaseElem[T](val e: T)

class OrderedElem[U <% Ordered[U]](override val e: U) extends BaseElem(e) with Ordered[OrderedElem[U]] {
  override def compare(that: OrderedElem[U]): Int = this.e.compare(that.e)
}

object Run extends App {
  val a = new OrderedElem(0)
  val b = new OrderedElem(1)
  println(a < b)  // => true
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Couldn't match expected type `Data.ByteString.Internal.ByteString' with actual type `ByteString'

From Dev

Couldn't match expected type 'Data.ByteString.Lazy.Internal.ByteString' with actual type '[Char]'

From Dev

Couldn't match expected type `Int' with actual type [Int]

From Dev

Couldn't match expected type `Maybe (String, Int, String)' with actual type `([Char], t0, [Char])'

From Dev

"Couldn't match type `Maybe' with `IO' Expected type: IO String Actual type: Maybe String" In Haskell

From Dev

Couldn't match expected type `Int' with actual type `m0 Int'

From Dev

Couldn't match expected type `IO ()' with actual type `a0 -> m0 a0'

From Dev

Couldn't match expected type `()' with actual type `Int'

From Dev

Couldn't match expected type with actual type

From Dev

Scala generics; Why do I get 'Type mismatch, expected: T, actual T'

From Dev

Haskell readFile: Couldn't match expected type ‘[String]’ with actual type ‘IO String’

From Dev

Haskell "Couldn't match expected type ‘a’ with actual type ‘[a0]’"

From Dev

Type mismatch: inferred type is T but kotlin.Any was expected

From Dev

How to solve Type mismatch issue (expected: Double, actual: Unit)

From Dev

Why isn't this a type mismatch?

From Dev

error Couldn't match expected type ‘Char’ with actual type ‘[Char]’

From Dev

Couldn't match expected type ‘a’ with actual type ‘Double’:

From Dev

Couldn't match expected type ‘r’ with actual type ‘Horse’

From Dev

Couldn't match expected type `Int' with actual type [Int]

From Dev

Couldn't match expected type `Maybe (String, Int, String)' with actual type `([Char], t0, [Char])'

From Dev

"Couldn't match type `Maybe' with `IO' Expected type: IO String Actual type: Maybe String" In Haskell

From Dev

Couldn't match expected type `()' with actual type `Int'

From Dev

Haskell 'Couldn't match expected type with actual type'

From Dev

Why isn't this a type mismatch?

From Dev

Couldn't match expected type ‘(Int, Int)’ with actual type ‘[t0]’

From Dev

Couldn't match expected type ‘[a]’ with actual type ‘a’

From Dev

Couldn't match expected type `a` with actual type `Integer`

From Dev

why it shows this error: Couldn't match expected type `[a]' with actual type `a'?

From Dev

Couldn't match expected type ‘Post’ with actual type ‘Route App’

Related Related

  1. 1

    Couldn't match expected type `Data.ByteString.Internal.ByteString' with actual type `ByteString'

  2. 2

    Couldn't match expected type 'Data.ByteString.Lazy.Internal.ByteString' with actual type '[Char]'

  3. 3

    Couldn't match expected type `Int' with actual type [Int]

  4. 4

    Couldn't match expected type `Maybe (String, Int, String)' with actual type `([Char], t0, [Char])'

  5. 5

    "Couldn't match type `Maybe' with `IO' Expected type: IO String Actual type: Maybe String" In Haskell

  6. 6

    Couldn't match expected type `Int' with actual type `m0 Int'

  7. 7

    Couldn't match expected type `IO ()' with actual type `a0 -> m0 a0'

  8. 8

    Couldn't match expected type `()' with actual type `Int'

  9. 9

    Couldn't match expected type with actual type

  10. 10

    Scala generics; Why do I get 'Type mismatch, expected: T, actual T'

  11. 11

    Haskell readFile: Couldn't match expected type ‘[String]’ with actual type ‘IO String’

  12. 12

    Haskell "Couldn't match expected type ‘a’ with actual type ‘[a0]’"

  13. 13

    Type mismatch: inferred type is T but kotlin.Any was expected

  14. 14

    How to solve Type mismatch issue (expected: Double, actual: Unit)

  15. 15

    Why isn't this a type mismatch?

  16. 16

    error Couldn't match expected type ‘Char’ with actual type ‘[Char]’

  17. 17

    Couldn't match expected type ‘a’ with actual type ‘Double’:

  18. 18

    Couldn't match expected type ‘r’ with actual type ‘Horse’

  19. 19

    Couldn't match expected type `Int' with actual type [Int]

  20. 20

    Couldn't match expected type `Maybe (String, Int, String)' with actual type `([Char], t0, [Char])'

  21. 21

    "Couldn't match type `Maybe' with `IO' Expected type: IO String Actual type: Maybe String" In Haskell

  22. 22

    Couldn't match expected type `()' with actual type `Int'

  23. 23

    Haskell 'Couldn't match expected type with actual type'

  24. 24

    Why isn't this a type mismatch?

  25. 25

    Couldn't match expected type ‘(Int, Int)’ with actual type ‘[t0]’

  26. 26

    Couldn't match expected type ‘[a]’ with actual type ‘a’

  27. 27

    Couldn't match expected type `a` with actual type `Integer`

  28. 28

    why it shows this error: Couldn't match expected type `[a]' with actual type `a'?

  29. 29

    Couldn't match expected type ‘Post’ with actual type ‘Route App’

HotTag

Archive