Created
April 17, 2013 09:22
-
-
Save milessabin/5402966 to your computer and use it in GitHub Desktop.
shapeless coproduct WIP
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
/* | |
* Copyright (c) 2013 Miles Sabin | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
*/ | |
package shapeless | |
import org.junit.Test | |
import org.junit.Assert._ | |
import test.illTyped | |
class CoproductTests { | |
def typed[T](t : => T) {} | |
object size extends Poly1 { | |
implicit val caseInt = at[Int](_ => 1) | |
implicit val caseString = at[String](_.length) | |
implicit val caseBoolean = at[Boolean](_ => 1) | |
} | |
@Test | |
def testBasics { | |
type ISB = Int :+: String :+: Boolean | |
type III = Int :+: Int :+: Int | |
val foo1 = Coproduct[ISB](23) | |
val foo2 = Coproduct[ISB]("foo") | |
val foo3 = Coproduct[ISB](true) | |
illTyped(""" | |
val foo4 = Coproduct[ISB](1.0) | |
""") | |
def cpMatch(v: ISB) = v match { | |
case Inl(x) => println(x) | |
case Inr(Inl(x)) => println(x) | |
case Inr(Inr(x)) => println(x) | |
} | |
cpMatch(foo1) | |
cpMatch(foo2) | |
cpMatch(foo3) | |
val foo1b = foo1 map size | |
typed[III](foo1b) | |
println(foo1b) | |
val foo2b = foo2 map size | |
typed[III](foo2b) | |
println(foo2b) | |
val foo3b = foo3 map size | |
typed[III](foo3b) | |
println(foo3b) | |
val foo1c = foo1b.unify | |
typed[Int](foo1c) | |
val foo2c = foo2b.unify | |
typed[Int](foo2c) | |
val foo3c = foo3b.unify | |
typed[Int](foo3c) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment