Home > Blockchain >  What does Scala mean by Suspicious Type Shadowing?
What does Scala mean by Suspicious Type Shadowing?

Time:05-13

I'm implementing a scala Set, and I'm getting this error from this code

Suspicious shadowing by a Type Parameter: A 
def remove[A](elemToRemove: A): MySet[A]
           ^ 

which for some reason, the language hates this generic A I'm passing it, why is that? and what does it mean by "Suspicious shadonwing"?

https://scastie.scala-lang.org/NwcMObgnSxGjXA2clmaEyA, though scastie is running into a different error, if you remove the [A] from remove[A] it will pass and execute

type mismatch;
 found   : exercises.part2afp.MySet[A(in class NonEmptySet)]
 required: exercises.part2afp.MySet[A(in method remove)]

CodePudding user response:

This is the context:

case class EmptySet[A]() extends MySet[A] {
  override def remove[A](elemToRemove: A): MySet[A] = this

The problem is that the A in remove[A] is a different type from the A in EmptySet[A].

If you want these to be different types, use different letters.

If it is supposed to be the same type, delete the [A] from remove.

  • Related