Generic Programming in Scala

依然范特西╮ 提交于 2019-11-29 16:55:19
senia

You should just remove type parameter from loop method. Replace loop[T] with loop.

With loop[T] you are creating new type parameter with name T, so T outside loop method and T in loop method are different type aliases with the same name.

It's called shadowing.

See these answers for similar problems:

  1. Scala type parameter error, not a member of type parameter
  2. Scala, Extend object with a generic trait
  3. Generic type inference in Scala

The problem is that with the inner function loop you are defining a new type T that is shadowing the outer type T.

The compiler sees them as defining different types. If you simply remove the T type parameter from loop (including the recursive call loop(list.tail, res)) you should find it compiles just fine.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!