Skip to content

Instantly share code, notes, and snippets.

@travisbrown
Last active December 18, 2015 03:58
Show Gist options
  • Save travisbrown/5721465 to your computer and use it in GitHub Desktop.
Save travisbrown/5721465 to your computer and use it in GitHub Desktop.
Specifying a type member in an implicit search.
scala> trait Foo { type X }
defined trait Foo
scala> implicit object foo extends Foo { type X = String }
defined module foo
scala> implicitly[Foo { type X = String }]
res0: Foo{type X = String} = foo$@6e1641c4
scala> implicitly[Foo]
res1: Foo = foo$@6e1641c4
scala> implicitly[Foo { type X = Char }]
<console>:10: error: could not find implicit value for parameter e: Foo{type X = Char}
implicitly[Foo { type X = Char }]
^
scala> import shapeless._
import shapeless._
scala> implicit def bar[H <: HList](h: H)(implicit len: Length[H] { type Out = Nat._2 }) = len
bar: [H <: shapeless.HList](h: H)(implicit len: shapeless.Length[H]{type Out = shapeless.Nat._2})shapeless.Length[H]{type Out = shapeless.Nat._2}
scala> bar(1 :: HNil)
<console>:14: error: could not find implicit value for parameter len: shapeless.Length[shapeless.::[Int,shapeless.HNil]]{type Out = shapeless.Nat._2}
bar(1 :: HNil)
^
scala> bar(1 :: 'a :: HNil)
res4: shapeless.Length[shapeless.::[Int,shapeless.::[Symbol,shapeless.HNil]]]{type Out = shapeless.Nat._2} = shapeless.Length$$anon$20@4b7cbfd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment