Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unwrap tag (tagged) #538

Merged
merged 1 commit into from
May 6, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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