0

This code compiles :

trait Plus[A] {
  def plus(a1 : A , a2: A): A
}

def plus[A: Plus](a1: A, a2: A): A = implicitly[Plus[A]].plus(a1, a2)
//> plus: [A](a1: A, a2: A)(implicit evidence$1: day0.sumfunction.Plus[A])A

But if I attempt to use :

def plus[A: Plus](a1: A, a2: A): A = implicit[Plus[A]].plus(a1, a2)

Then I receive compile error : Multiple markers at this line - missing parameter type - identifier expected but '[' found.

Why can't I use implicit in this case ?

This code is taken from http://eed3si9n.com/learning-scalaz/polymorphism.html

Ende Neu
  • 15,581
  • 5
  • 57
  • 68
blue-sky
  • 51,962
  • 152
  • 427
  • 752
  • 2
    I don't think the `implicit` keyword takes a type parameter, the `implicitly` method does: `@inline def implicitly[T]`. – Ende Neu Sep 20 '14 at 20:51
  • What encouraged you to attempt such a thing? – som-snytt Sep 20 '14 at 21:11
  • @som-snytt i did share where the code originated, a scalaz tutorial : http://eed3si9n.com/learning-scalaz/polymorphism.html – blue-sky Sep 20 '14 at 21:13
  • 1
    Specifically, I was curious if you read something that was confusing in this particular way. Duplicates to just not knowing are http://stackoverflow.com/questions/22669233/scala-implicitly-vs-implicit-arguments or http://stackoverflow.com/questions/3855595/what-is-the-scala-identifier-implicitly – som-snytt Sep 20 '14 at 23:28

1 Answers1

3

implicit is a keyword that cannot be used like that. You are looking for implicitly, which resides in Predef:

def plus[A: Plus](a1: A, a2: A): A = implicitly[Plus[A]].plus(a1, a2)

Or alternatively:

def plus[A](a1: A, a2: A)(implicit x: Plus[A]): A = x.plus(a1, a2)