Skip to content

Instantly share code, notes, and snippets.

@wheaties
Last active August 29, 2015 14:08
Show Gist options
  • Save wheaties/3567607f1fdfbfc3c52d to your computer and use it in GitHub Desktop.
Save wheaties/3567607f1fdfbfc3c52d to your computer and use it in GitHub Desktop.
Is Impls
trait IsBase[F]{
type T
type W[X]
def to(f: F): W[T]
def from(wt: W[T]): F
def apply(f: F): W[T] = to(f)
}
trait IsFuture[F] extends IsBase[F]{
type W[X] = Future[X]
}
object IsFuture{
def unapply[F](f: F)(implicit isf: IsFuture[F]): Option[Future[isf.T]] = Some(isf(f))
def apply[F](implicit isf: IsFuture[F]): Aux[F, isf.T] = isf
type Aux[FA, A] = IsFuture[FA]{ type T = A }
implicit def mk[A] = new IsFuture[Future[A]]{
type T = A
def from(wt: W[T]) = wt
def to(f: Future[A]) = f
}
}
trait IsTry[F] extends IsBase[F]{
type W[X] = Try[X]
}
object IsTry{
def unapply[F](f: F)(implicit ist: IsTry[F]): Option[Try[ist.T]] = Some(ist(f))
def apply[F](implicit ist: IsTry[F]): Aux[F, ist.T] = ist
type Aux[FA, A] = IsTry[FA]{ type T = A }
implicit def mk[A] = new IsTry[Try[A]]{
type T = A
def from(wt: W[T]) = wt
def to(f: F) = f
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment