In Scala 3, let's say I have a List[Try[String]]. Can I split it up into success and failures, such that each list has the appropriate subtype?
If I do the following:
import scala.util.{Try, Success, Failure}
val tries = List(Success("1"), Failure(Exception("2")))
val (successes, failures) = tries.partition(_.isSuccess)
then successes and failures are still of type List[Try[String]]. The same goes if I filter based on the type:
val successes = tries.filter(_.isInstanceOf[Success[String]])
I could of course cast to Success and Failure respectively, but is there a type-safe way to achieve this?
In Scala 2 I would do something like
https://scastie.scala-lang.org/DmytroMitin/uQp603sXT7WFYmYntDXmIw/1
I managed to translate this code into Scala 3 although the translation turned to be wordy (I remplemented
GenericandCoproduct)Scala 3.0.2
In the macro (generating pattern matching)
Inferred(typeRepr)should be instead ofTypeIdent(typeRepr.typeSymbol), otherwise this doesn't work for parametric case classes. Actually the macro can be removed at all if we usemirror.ordinal. Simplified version isReplacing type classes with compile-time/inline methods and match types
Tested in 3.2.0 https://scastie.scala-lang.org/DmytroMitin/940QaiqDQQ2QegCyxTbEIQ/1
How to access parameter list of case class in a dotty macro
Alternative implementation of
loopAnother implementation for Scala 2: Split list of algebraic date type to lists of branches?