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

Mapping over HLIST with poly from different input type to same return type does not compile #73

Closed
drexin opened this issue Jan 15, 2014 · 2 comments

Comments

@drexin
Copy link
Contributor

drexin commented Jan 15, 2014

Hi,

writing a poly function like this:

object testfun extends Poly1 {
  implicit val atString = at[String](identity)
  implicit val atInt = at[Int](_.toString)
  implicit def default[A] = at[A](identity)
}

and trying to map over an HList leads to following error:

val hlist = 1 :: "foo" :: 1.5 :: HNil

hlist map testfun

[error] /Users/dario.rexin/projects/shapeless-foo/src/main/scala/Main.scala:15: could not find implicit value for parameter mapper: shapeless.ops.hlist.Mapper[main.scala.Test.testfun.type,shapeless.::[Int,shapeless.::[String,shapeless.::[Double,shapeless.HNil]]]]
[error]   hlist map testfun
@milessabin
Copy link
Owner

Sorry it's taken me so long to get back to you on this. What you're seeing is the result of quite ordinary implicit ambiguity due to the overlap of the result types between your default case and the other two cases. The fix for this is the same as for any other case of implicit ambiguity of this sort: move the offending implicit definition to a low priority super-trait,

trait lpTestfun extends Poly1 {
  implicit def default[A] = at[A](identity)
}

object testfun extends lpTestFun {
  implicit val atString = at[String](identity)
  implicit val atInt = at[Int](_.toString)
}

Now mapping this over an HList will do exactly what you want,

scala> hlist map testfun
res0: testfun.atInt.Result :: testfun.atString.Result :: Double :: HNil =
  1 :: foo :: 1.5 :: HNil

@drexin
Copy link
Contributor Author

drexin commented Feb 16, 2014

Ah, of course. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants