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

Ezward

I'm working through the Coursera course, Functional Programming Principles in Scala. I'm taking the IntSet example from week3 and attempting to make it use generics. Generics have only been covered very briefly at this point, so I'm probably doing something obviously wrong, but it is not clear to me. I started by make T <: Comparable, but this I found Ordered and so I'm attempting to require values in the set to be ordered. The issue is that I get a 'Type mismatch, expected: T, actual T' error in a couple of places; I've commented the lines in the source below. That is a strange error; it found the expected type but that is an error? Note: This is NOT an assignment so I am not asking anyone to take a test for me. I just wanted to go back and make the Set type more useful and I ran into this unexpected behaviorenter code here. Thank you for your help.

package week3

trait Set[T <: Ordered]
{
  def isEmpty: Boolean
  def contains(i: T): Boolean
  def include(i: T): Set[T]
  def union(that: Set[T]): Set[T]
}


/**
 * empty set
 */
class EmptySet[T <: Ordered] extends Set[T]
{
  def isEmpty = true;

  def contains(i: T): Boolean = false

  def include(i: T): Set[T] =
    new TreeSet(i, new EmptySet[T], new EmptySet[T])

  def union(that: Set[T]): Set[T] = that

  override def toString() = "{}"
}


/**
 * Immutable set
 *
 * @param value
 * @param left
 * @param right
 */
class TreeSet[T <: Ordered] (value: T, left: Set[T], right: Set[T]) extends Set[T]
{
  def isEmpty = false;

  def this(v: T) = this(v, new EmptySet[T], new EmptySet[T])

  def contains(v: T): Boolean =
  {
    if(v < value) left.contains(v)       // Type mismatch, expected: T, actual T
    else if(v > value) right.contains(v) // Type mismatch, expected: T, actual T
    else true
  }

  def include(v: T): Set[T] =
  {
    if(v < value) new TreeSet(value, left.include(v), right)      // Type mismatch, expected: T, actual T
    else if(v > value) new TreeSet(value, left, right.include(v)) // Type mismatch, expected: T, actual T
    else this
  }

  def union(that: Set[T]): Set[T] =
  {
    if(that.isEmpty) this
    else if(that == this) this
    else ((left union right) union that) include value
  }

  override def toString() = "{ " + left.toString + ' ' + value + ' ' +  right.toString + " }"
}
Sergii Lagutin

Ordered has parameter type too. You should use it as Ordered[T] to fix it. And Set type should be T <: Ordered[T]. It's not Scala's problem. It's correct way of usage java's Comparable interface.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Type mismatch, expected: T, actual: T

From Dev

Java generics - type mismatch from T to T

From Dev

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

From Dev

Scala type mismatch with generics

From Dev

Why isn't this a type mismatch?

From Dev

Why isn't this a type mismatch?

From Dev

Why do I get "expected class or object definition" when defining a type in scala?

From Dev

Couldn't match expected type with actual type

From Dev

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

From Dev

Scala: Type mismatch with nested generics

From Java

Scala Type Mismatch, Can't Resolve the Problem

From Dev

Why isn't the expected function called? Do I understand type traits incorrectly?

From Dev

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

From Dev

Why isn't this assignment a type mismatch?

From Dev

Scala Generics Type Bounds - Pointing to the Actual Type

From Dev

Java/Scala Bounded Generics and type inference mismatch

From Dev

JAVA Why do I get a type error when assigning these variables (T extends Comparable<T>)

From Dev

Scala existentials - type mismatch, unable to infer T =:= T

From Dev

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

From Dev

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

From Dev

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

From Dev

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

From Dev

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

From Dev

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

From Dev

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

From Dev

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

From Dev

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

From Dev

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

From Dev

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

Related Related

  1. 1

    Type mismatch, expected: T, actual: T

  2. 2

    Java generics - type mismatch from T to T

  3. 3

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

  4. 4

    Scala type mismatch with generics

  5. 5

    Why isn't this a type mismatch?

  6. 6

    Why isn't this a type mismatch?

  7. 7

    Why do I get "expected class or object definition" when defining a type in scala?

  8. 8

    Couldn't match expected type with actual type

  9. 9

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

  10. 10

    Scala: Type mismatch with nested generics

  11. 11

    Scala Type Mismatch, Can't Resolve the Problem

  12. 12

    Why isn't the expected function called? Do I understand type traits incorrectly?

  13. 13

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

  14. 14

    Why isn't this assignment a type mismatch?

  15. 15

    Scala Generics Type Bounds - Pointing to the Actual Type

  16. 16

    Java/Scala Bounded Generics and type inference mismatch

  17. 17

    JAVA Why do I get a type error when assigning these variables (T extends Comparable<T>)

  18. 18

    Scala existentials - type mismatch, unable to infer T =:= T

  19. 19

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

  20. 20

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

  21. 21

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

  22. 22

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

  23. 23

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

  24. 24

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

  25. 25

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

  26. 26

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

  27. 27

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

  28. 28

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

  29. 29

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

HotTag

Archive