2

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 :-)

ryskajakub
  • 6,351
  • 8
  • 45
  • 75
  • 1
    `Either` is just a particular sum type with two constructors. You can write your own sum types (with as many constructors as you want; aka. algebraic data types) as a set of case classes or objects that extend a sealed trait. – Travis Brown May 21 '13 at 11:08

1 Answers1

1

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.