Skip to content

Commit

Permalink
Merge pull request #538 from etaty/features/tagged-unwrap
Browse files Browse the repository at this point in the history
unwrap tag (tagged)
  • Loading branch information
milessabin committed May 6, 2016
2 parents 19ce7ea + 2f1f5bf commit c332f90
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
7 changes: 7 additions & 0 deletions core/src/main/scala/shapeless/unwrapped.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package shapeless

import newtype._
import shapeless.tag._

trait Unwrapped[W] extends Serializable {
type U
Expand Down Expand Up @@ -60,6 +61,12 @@ trait UnwrappedInstances extends LowPriorityUnwrappedInstances {
chain: Strict[Unwrapped.Aux[UI, UF]]
) = chain.value.asInstanceOf[Unwrapped.Aux[Newtype[UI, Ops], UF]]

implicit def taggedUnwrapped[UI, T]: Unwrapped.Aux[UI @@ T, UI] =
new Unwrapped[UI @@ T] {
type U = UI
def unwrap(w: UI @@ T) = w.asInstanceOf[UI]
def wrap(u: UI) = tag[T](u)
}
}

trait LowPriorityUnwrappedInstances {
Expand Down
14 changes: 14 additions & 0 deletions core/src/test/scala/shapeless/unwrapped.scala
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,20 @@ class UnwrappedTests {
test.illTyped("unwrapped: String @@ TestTag")
}

@Test
def testShapelessTagged: Unit = {
import shapeless.tag.@@
import syntax.unwrapped._

type Test = String @@ TestTag
val tagged = tag[TestTag]("testing")

val wrapped = "testing".wrap[Test]
val unwrapped = wrapped.unwrap
assert((wrapped: String @@ TestTag) == tagged)
assert((unwrapped: String) == "testing")
}

@Test
def testAlreadyUnwrapped: Unit = {

Expand Down

0 comments on commit c332f90

Please sign in to comment.