Skip to content

Commit

Permalink
Issue 41: Added support for Tagging and changed the Numeric <-> Strin…
Browse files Browse the repository at this point in the history
…g Bijections to use String @@ Rep[Numeric] as proof.
  • Loading branch information
Jed Wesley-Smith committed Jan 9, 2013
1 parent 1586795 commit 059f41d
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,40 +70,40 @@ trait NumericBijections {
/**
* Bijections between the numeric types and string.
*/
implicit val byte2String: Bijection[Byte, String] =
new Bijection[Byte, String] {
def apply(b: Byte) = b.toString
override def invert(s: String) = s.toByte
implicit val byte2String: Bijection[Byte, String @@ Rep[Byte]] =
new Bijection[Byte, String @@ Rep[Byte]] {
def apply(b: Byte) = Tag(b.toString)
override def invert(s: String @@ Rep[Byte]) = s.toByte
}

implicit val short2String: Bijection[Short, String] =
new Bijection[Short, String] {
def apply(s: Short) = s.toString
override def invert(s: String) = s.toShort
implicit val short2String: Bijection[Short, String @@ Rep[Short]] =
new Bijection[Short, String @@ Rep[Short]] {
def apply(s: Short) = Tag(s.toString)
override def invert(s: String @@ Rep[Short]) = s.toShort
}

implicit val int2String: Bijection[Int, String] =
new Bijection[Int, String] {
def apply(i: Int) = i.toString
override def invert(s: String) = s.toInt
implicit val int2String: Bijection[Int, String @@ Rep[Int]] =
new Bijection[Int, String @@ Rep[Int]] {
def apply(i: Int) = Tag(i.toString)
override def invert(s: String @@ Rep[Int]) = s.toInt
}

implicit val long2String: Bijection[Long, String] =
new Bijection[Long, String] {
def apply(l: Long) = l.toString
override def invert(s: String) = s.toLong
implicit val long2String: Bijection[Long, String @@ Rep[Long]] =
new Bijection[Long, String @@ Rep[Long]] {
def apply(l: Long) = Tag(l.toString)
override def invert(s: String @@ Rep[Long]) = s.toLong
}

implicit val float2String: Bijection[Float, String] =
new Bijection[Float, String] {
def apply(f: Float) = f.toString
override def invert(s: String) = s.toFloat
implicit val float2String: Bijection[Float, String @@ Rep[Float]] =
new Bijection[Float, String @@ Rep[Float]] {
def apply(f: Float) = Tag(f.toString)
override def invert(s: String @@ Rep[Float]) = s.toFloat
}

implicit val double2String: Bijection[Double, String] =
new Bijection[Double, String] {
def apply(d: Double) = d.toString
override def invert(s: String) = s.toDouble
implicit val double2String: Bijection[Double, String @@ Rep[Double]] =
new Bijection[Double, String @@ Rep[Double]] {
def apply(d: Double) = Tag(d.toString)
override def invert(s: String @@ Rep[Double]) = s.toDouble
}

/**
Expand Down
6 changes: 6 additions & 0 deletions bijection-core/src/main/scala/com/twitter/bijection/Rep.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.twitter.bijection

/**
* Type tag used when a type such as String may contain representations of another type, such as Int or URL.
*/
trait Rep[A]
20 changes: 20 additions & 0 deletions bijection-core/src/main/scala/com/twitter/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,24 @@ package com.twitter
* libraries (Bijection[MyTrait, YourTrait]) and many other purposes.
*/
package object bijection {

/**
* Tagging infrastructure.
*/
type Tagged[T] = { type Tag = T }

/**
* Tag a type `T` with `Tag`. The resulting type is a subtype of `T`.
*
* The resulting type is used to discriminate between type class instances.
*/
type @@[T, Tag] = T with Tagged[Tag]

private[bijection] object Tag {
@inline def apply[A, T](a: A): A @@ T = a.asInstanceOf[A @@ T]

def subst[A, F[_], T](fa: F[A]): F[A @@ T] = fa.asInstanceOf[F[A @@ T]]

def unsubst[A, F[_], T](fa: F[A @@ T]): F[A] = fa.asInstanceOf[F[A]]
}
}

0 comments on commit 059f41d

Please sign in to comment.