Skip to content

Instantly share code, notes, and snippets.

@jedws
Last active December 19, 2015 06:49
Show Gist options
  • Save jedws/5914678 to your computer and use it in GitHub Desktop.
Save jedws/5914678 to your computer and use it in GitHub Desktop.
Parameterized type tags
import scalaz._
import Scalaz._
import java.lang.{Long => JLong}
object ParameterizedTags {
trait Nanoseconds[A]
type SampleDuration[A] = JLong @@ Nanoseconds[A]
case class Bar(a: BarDuration)
type BarDuration = SampleDuration[Bar]
case class Foo(a: FooDuration)
type FooDuration = SampleDuration[Foo]
val f: FooDuration = Tag(1L)
val b: BarDuration = Tag(1L)
// does not compile
// val ff: FooDuration = b
implicit val JLongMonoid =
new Monoid[JLong] {
private val M = Monoid[Long]
def zero: JLong = M.zero
def append(f1: JLong, f2: => JLong): JLong =
M.append(f1, f2)
}
implicit def DurationMonoid[A] =
new Monoid[SampleDuration[A]] {
private val M = Monoid[JLong]
def zero: SampleDuration[A] = Tag(M.zero)
def append(f1: SampleDuration[A], f2: => SampleDuration[A]): SampleDuration[A] =
Tag(M.append(f1, f2))
}
//val m = Monoid[Long]
val m = Monoid[FooDuration]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment