Skip to content

Instantly share code, notes, and snippets.

@retronym
Created December 9, 2009 06:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save retronym/252311 to your computer and use it in GitHub Desktop.
Save retronym/252311 to your computer and use it in GitHub Desktop.
Trying to update scalaz Pure typeclass to use GenericCompanion.apply...
class Scratch
import collection.generic.GenericCompanion
trait HasGenericCompanion[S[X] <: Traversable[X]] {
def companion: GenericCompanion[S]
}
object HasGenericCompanion {
lazy implicit val StreamHasCompanion: HasGenericCompanion[Stream] = new HasGenericCompanion[Stream] {
def companion = Stream
}
lazy implicit val ListHasCompanion: HasGenericCompanion[List] = new HasGenericCompanion[List] {
def companion = List
}
}
trait Pure[+P[_]] {
def pure[A](a: => A): P[A]
}
object Pure {
implicit def HasGenericCompanionPure[S[_]](implicit c: HasGenericCompanion[S]) = new Pure[S] {
def pure[A](a: => A) = {
c.companion.apply(a)
}
}
}
implicitly[HasGenericCompanion[Stream]]
val pure = Pure.HasGenericCompanionPure(implicitly[HasGenericCompanion[Stream]])
val pure2 = Pure.HasGenericCompanionPure[Stream]
// this fails with:
//$anon.this.Pure.HasGenericCompanionPure is not a valid implicit value for this.Pure[Stream] because:
//ambiguous implicit values:
// both lazy value ListHasCompanion in object HasGenericCompanion of type => this.HasGenericCompanion[List]
// and lazy value StreamHasCompanion in object HasGenericCompanion of type => this.HasGenericCompanion[Stream]
// match expected type this.HasGenericCompanion[S]
//(fragment of Scratch.scala):43: error: could not find implicit value for parameter e: this.Pure[Stream]
implicitly[Pure[Stream]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment