EECOLOR
EECOLOR

Reputation: 11244

Is there a way to distinguish between a case class and a Tuple?

I wonder if there is a way to distinguish for implicit conversions without listing 22 options of TupleN?

implicit def forTuple[A <: Product](a:A) = createForTuple(a)
implicit def forCaseClass[A](a:A) = createForCaseClass(a)

The forTuple method is selected for case classes because they extend product as well.

Upvotes: 3

Views: 180

Answers (1)

Connor Doyle
Connor Doyle

Reputation: 1912

You can do this with reflection as shown below (if you're using scala version 2.10):

import scala.reflect.runtime.universe._

def forEither[P <: Product : TypeTag](product: P) = {
  if (typeOf[P].typeSymbol.asClass.isCaseClass) forCaseClass(product)
  else forTuple(product)
}

Upvotes: 2

Related Questions