2013-09-12

xacid: (Default)
2013-09-12 09:03 am
Entry tags:

О! ... ?

object o_O extends App {

  import shapeless._, ops.coproduct._

  type ** = Int :+: String :+: Boolean :+: CNil

  implicit def *>[?](x: ?)(implicit %% : ** Inject ?) = %%(x)

  def ?=>(x: **) = x unify match {
    case n: Int => n
    case s: String => s.length
    case b: Boolean => if (b) 1 else 0
  }

  def +?(x: (**)*) = x map ?=> reduce (_ + _)

  // +?(1,"hello", true, 2.0)
  // Compiler forbids: Wrong type: 2.0 : Double

  println {

     +?(1, "hello", true)) // 7

  }

}