Yaneeve
Yaneeve

Reputation: 4779

Implicit arguments: how to encode in function signature?

Following the awesomely enlightening question by @TravisBrown concerning the enumeration of ADTs using shapeless, I am left with the following code snippet:

implicitly[EnumerableAdt[Foo]].values

I would like to encapsulate this within a method so that I don't have to .values after each invocation (It seems a cleaner API to me, that way). But I can't seem to get it right. Whenever I try to encapsulate the implicitly[EnumerableAdt[Foo]] I get implicit resolution errors.

What I had tried, that made most sense to me, was, for example:

def imply[T](implicit ev: T):Set[T] = implicitly[EnumerableAdt[T]].values

certainly without the ev made even less sense to me.

I am no expert in type level programming.

Upvotes: 1

Views: 160

Answers (1)

OlivierBlanvillain
OlivierBlanvillain

Reputation: 7768

If you look at the definition of implicitly[X], you can see that is requires an implicit argument of type X in scope. In your example, you have implicit ev: T in scope, which is not enough to call implicitly[EnumerableAdt[T]]! Try the following definition instead:

def imply[T](implicit ev: EnumerableAdt[T]):Set[T] = ev.values

Upvotes: 1

Related Questions