简体   繁体   中英

Value :: is not a member of type parameter [T] Scala?

For a lister class that I have to make, which is basically just a list with some extra methods I have to have an intersection method. I think what I have below works but at the part that I labeled problem line I keep getting a "value :: is not a member of type parameter S" error. What would be causing this? Second part is just bonus but if anyone could say why what I labeled problem line two keeps saying ambiguous refrence to overloaded definition that would be great. There are two constructors, one which takes one element and starts the list, which is public, and a private main constructor that can take a list of type s and make a lister.

def intersec(other:List[S]) = {
val a=this.toList
val b=other.toList
var holder = List()
var counter=b.length
if (b.length<a.length)cycles=a.length
for (i<-0 to cycles){
  if (a.contains(b(i))){
    holder=holder::b(i)       // problem line
  }
new Lister(holder)      // problem line 2
}}

b(i) has type S and of course type S does not have prepend function. Notice that :: is right associative. So holder :: b(i) means that you are calling :: function of b(i) with parameter holder .

So you need to change that line to: holder = b(i) :: holder

By the way, your code is quite imperative and suffers from lack of being declarative. I think you want to have an intersect function with different behaviour than the Scala standard library intersect function. In this case, you may use implicit class to extend the List to have new function.

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