简体   繁体   中英

What does Scala mean by Suspicious Type Shadowing?

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)]

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM