Converting splited string array to a set in Scala

At the next Scala function:

def usersForDepartments(colDeptIds: Option[String]) = Action.async {
    request => {
                 println(colDeptIds)
                 for {
                        users <- Users.getAll(u => u.department in (colDeptIds.getOrElse("").split(";").toSet))
                      }    yield Ok( Json.stringify(convertUsersToJsonOrig(users)))
                }
     }

colDeptIds will have values like: “001”, “001;002;003”. i.e. it will have a string containing numeric ids like 001, 002, separated by “;”.
Each User u has a field called department. The goal of this function is to get all the users with a department value “contained” in colDeptIds but that is not a proper sub-string of any id in that string. To solve this problem I am trying to convert colDeptIds to a set using:

(colDeptIds.getOrElse("").split(";").toSet)

but I get the error:

  scala:82: polymorphic expression cannot be instantiated to expected type;
  [error]  found   : [B >: String]scala.collection.immutable.Set[B]
  [error]  required: slick.lifted.Query[slick.lifted.Rep[?], ?, ?]
  [error]                                         users <- Users.getAll(u => u.department in (colDeptIds.getOrElse("").split(";").toSet))

Any help please? This Scala stuff can be confusing!

I think you need to give us more to go on. You’re getting a Slick error there, so we might need to see more of the code to really help you out.

If for instance the colDeptIds were defined as in the code below, you would not get an error from that expression you are saying gives an error now. And actually I don’t think you do have an error in that part of the line. The compiler is telling you that you have a Set[String] as expected (see my results below), what you do not have is a slick.lifted.Query[slick.lifted.Rep[?], ?, ?]

val colDeptIds = Option("001;002;003;001")
colDeptIds.getOrElse("").split(";").toSet
colDeptIds.fold(Array.empty[String])(_.split(";")).toSet
res0: scala.collection.immutable.Set[String] = Set(001, 002, 003)
res1: scala.collection.immutable.Set[String] = Set(001, 002, 003)

( I gave that 2nd way to handle that Option using fold because it might be seen as a more type-safe way to say what the default behavior will be if the option ends up wrapping some type you’re not expecting, fold can catch it at compile time )

It’s hard to say without having access to your code, but I’d bet this is
due to Set's variance - or lack thereof. Whatever magic Slick is
doing probably doesn’t help.

Try factoring out your Set to a val, like

val colDeptIdsSet = colDeptIds.getOrElse("").split(";").toSet

and adding a type annotation if that doesn’t work:

val colDeptIdsSet: Set[String] = colDeptIds.getOrElse("").split(";").toSet