Skip to content

Instantly share code, notes, and snippets.

@wheaties
Last active August 29, 2015 14:05
Show Gist options
  • Select an option

  • Save wheaties/b6c7a30a76fdd15feddc to your computer and use it in GitHub Desktop.

Select an option

Save wheaties/b6c7a30a76fdd15feddc to your computer and use it in GitHub Desktop.
StackOverflow at compile time, Attempt to add OrElse to Poly
//This is all I'm adding...
object PolyDefns extends Cases{
//stuff
//.
//.
//.
//my stuff
class OrElse[F, G](f: F, g: G) extends Poly
object OrElse extends LowPriorityOrElseImplicits {
implicit def orElseCase[C, F <: Poly, G, L <: HList](implicit unpack: Unpack2[C, OrElse, F, G], ev: Case[F, L]): Case.Aux[C, L, ev.Result] =
new Case[C, L]{
type Result = ev.Result
val value = ev.value
}
}
trait LowPriorityOrElseImplicits {
implicit def orElseCase2[C, F, G <: Poly, L <: HList](implicit unpack: Unpack2[C, OrElse, F, G], ev: Case[G, L]): Case.Aux[C, L, ev.Result] =
new Case[C, L]{
type Result = ev.Result
val value = ev.value
}
}
}
//with declaration within Poly as
def orElse(f: Poly) = new OrElse[this.type, f.type](this, f)
//Within the unit tests
object one extends Poly3{
implicit val iii = at[Int, Int, Int]{
case (x, y, z) => x+y+z
}
implicit val iid = at[Int, Int, Double]{
case (x, y, z) => 1.0
}
}
object two extends Poly3{
implicit val iid = at[Int, Int, Double]{
case (x, y, z) => 2.0
}
implicit val idd = at[Int, Double, Double]{
case (x, y, z) => 2.0
}
}
@Test
def testOrElse{
{ //Test with similar arity
val gen = one orElse two
assertTypedEquals[Int](gen(1, 1, 1), 3)
assertTypedEquals[Double](gen(1, 1, 1.0), 1.0)
assertTypedEquals[Double](gen(1, 1.0, 1.0), 2.0)
}
{ //Test with different arity
val gen = bidi orElse one
assertTypedEquals[Int](gen("1"), 1)
assertTypedEquals[Int](gen(1, 1, 1), 3)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment