Search code examples
scalaobjectapache-sparkerror-handlinglarge-data

call method between two objects SCALA


I have the following two methods, how can I call the method defined in DocsObj into DistanceObj?

The first object is:

object DocsObj{
     def Docs(s: List[String], b:Int): List[String] = { ... }
}

The second one is:

object Distance{
    def tanimoto(l1: List[String], l2: List[String]): Float={
        var list1= List[String]()
        list1=DocsObj.Docs(l1,6).asInstanceOf[List].toSet
    }
}

the error is the following one:

<console>:29: error: polymorphic expression cannot be instantiated to expected type;
found   : [B >: String]scala.collection.immutable.Set[B]
required: List[String]
   list1=DocsObj.Docs(list1,6).asInstanceOf[List[String]].toSet

Solution

  • The problem is that the value you are attempting to assign to list1 is a Set, but the list1 var is of type List.

    The compiler error explains this as best it can, but perhaps the error message is a bit obscure because the right hand side of the assignment could take many types ("polymorphic expression"), so the "found" line in the error message is a range of types, rather than a single type.

    Try either changing list1 to be of type Set[String] or changing the toSet call to a toList call.