I have a vague feeling that such a method could exist (possibly in Cats?) - the purpose would be to turn an Iterable[T] (or Set[T] is what I'm actually interested in) into an Option[Iterable[S]] - the result being:
Some[Iterable[S]]: if all entries in theIterable[T]were an instance ofSNone: if any entries in theIterable[T]were NOT an instance ofS
I know there's a sort-of-similar method already available in Cats, granted by Alternative - it's separate:
import cats.implicits._
import alleycats.std.set._
val stringsAndInts: Set[Either[String, Int]] = Set(Right(6),Left("Foo"))
val (strings: Set[String], ints: Set[Int]) = stringsAndInts.separate
...that works nicely for a Set[Either[A,B] - but for this question I'm just interested in any old Set[T].
What about collect?
This code using the Scala standard library method collect will compile - but note that it will always produce a Set[S] - even if some of the setOfT weren't instances of S:
val setOfT: Set[T] = ???
val setOfS: Set[S] = setOfT.collect {case s: S => s}