Last active
August 29, 2015 14:05
-
-
Save wheaties/b6c7a30a76fdd15feddc to your computer and use it in GitHub Desktop.
StackOverflow at compile time, Attempt to add OrElse to Poly
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //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