Help needed with compilation error

61 views
Skip to first unread message

Abdul Rahman Sattar

unread,
Oct 22, 2016, 3:31:24 PM10/22/16
to scala-language
Hi, 

Below are my method signatures and definitions in scala

  def accumulate[T[_]: Traversable, O: Monoid, A]: (A => O) => T[A] => O =
    fao => ta =>
      (implicitly[Traversable[T]].traverse[({type f[X] = Acc[O, X]})#f, A, O](ta)(a => Acc(fao(a)))).value

  def reduce[T[_]: Traversable, O: Monoid]: T[O] => O = to => accumulate[T, O, O](a => a)(to)

However I get the following error for my definition of reduce

Error:(160, 82) not enough arguments for method accumulate: (implicit evidence$7: Traversable[T], implicit evidence$8: Monoid[O])(O => O) => (T[O] => O).
Unspecified value parameter evidence$8.
  def reduce[T[_]: Traversable, O: Monoid]: T[O] => O = to => accumulate[T, O, O](a => a)(to)
                                                                                 ^

Not sure where I am going wrong. Any help would be appreciated.

Thanks!

Andrew Phillips

unread,
Oct 22, 2016, 4:07:10 PM10/22/16
to scala-language
Hi Abdul

I suspect this may be more of a question for scala-user, but from a quick look, and without much investigation, it seems the problem is that the compiler interprets the expression accumulate[T, O, O](a => a)(to) as if you were trying to pass explicit arguments for the implicit parameters of the method accumulate. The method takes two implicit parameters, but the first argument list we are providing contains only one (a => a), so the compiler complains.

The following variants, which help the compiler understand that we are first trying to "create a version" of the accumulate method with the type parameters bound to T, O and O, then apply the arguments "a => a" and "to" to it, both work for me:

scala>   def reduce[T[_]: Traversable, O: Monoid]: T[O] => O = to => {
     |     val accumulateFun: (O => O) => T[O] => O = accumulate _
     |     accumulateFun(a => a)(to)
     |   }
warning: there was one feature warning; re-run with -feature for details
reduce: [T[_], O](implicit evidence$1: Traversable[T], implicit evidence$2: Mono
id[O])T[O] => O

scala>   def reduce[T[_]: Traversable, O: Monoid]: T[O] => O = to => accumulate[
T, O, O].apply(a => a)(to)
warning: there was one feature warning; re-run with -feature for details
reduce: [T[_], O](implicit evidence$1: Traversable[T], implicit evidence$2: Mono
id[O])T[O] => O

Note that both variants result in a warning on 2.11.6:

<console>:10: warning: higher-kinded type should be enabled
by making the implicit value scala.language.higherKinds visible.
This can be achieved by adding the import clause 'import scala.language.higherKi
nds'
or by setting the compiler option -language:higherKinds.
See the Scala docs for value scala.language.higherKinds for a discussion
why the feature should be explicitly enabled.

See http://scalapuzzlers.com/#pzzlr-044 for a related code sample.

Regards

ap


On Saturday, 22 October 2016 15:31:24 UTC-4, 
Reply all
Reply to author
Forward
0 new messages