Question

Is there implemented variable parameter count scala Either in some library, I mean something analogic to HList. I don't want to implement it by myself :-)

Was it helpful?

Solution

This is not directly an answer to your question but have you considered using Scalaz's Either type symbol \/ ? With that you can "chain" several types together in a sum type, like this:

import scalaz._

lazy val a: Int \/ String = ???
// a: scalaz.\/[Int,String] = <lazy>

lazy val b: Int \/ String \/ Double = ???
// b: scalaz.\/[scalaz.\/[Int,String],Double] = <lazy>

lazy val c: Int \/ String \/ Double \/ BigInt = ???
// c: scalaz.\/[scalaz.\/[scalaz.\/[Int,String],Double],BigInt] = <lazy>



val d1: Int \/ String \/ Double \/ BigInt = -\/(\/-(42d))
// d1: scalaz.\/[scalaz.\/[scalaz.\/[Int,String],Double],BigInt] = -\/(\/-(42d))

import Scalaz._

val d2: Int \/ String \/ Double \/ BigInt = 42d.right.left
// d2: scalaz.\/[scalaz.\/[scalaz.\/[Int,String],Double],BigInt] = -\/(\/-(42d))



val e1: Int \/ String \/ Double \/ BigInt = -\/(-\/(\/-("42")))
// e1: scalaz.\/[scalaz.\/[scalaz.\/[Int,String],Double],BigInt] = -\/(-\/(\/-("42")))

import Scalaz._

val e2: Int \/ String \/ Double \/ BigInt = "42".right.left.left
// e2: scalaz.\/[scalaz.\/[scalaz.\/[Int,String],Double],BigInt] = -\/(-\/(\/-("42")))

It also has extractors so you can pattern match these things.

But if you have all the involved types under your control, rolling your own algebraic data type (as outlined in Travis Brown's comment above) is probably the better solution.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top