2

Given the following code:

class Foo[R](i: Int)(implicit ev: Ordering[R]) {
  final type T = ev.type
}

I get the following error:

Error:(13, 16) private value ev escapes its defining scope as part of type Foo.this.ev.type type T = ev.type

Which makes me think implicits declared in a constructor are private. Given that T is final it won't be overridable, so it shouldn't cause any problem. What am I missing here?

Simão Martins
  • 1,210
  • 11
  • 22

2 Answers2

6

All parameters declared in a class constructor are private unless you tell the compiler they are not. This differs from a case class where all parameters in the first argument list are by default public unless you tell the compiler otherwise.

So, yes, unless you specifically add val or some other public-like modifier to the value, it is by default private. Hence, it is telling you that a private member is being made public via the way you are defining that type.

wheaties
  • 35,646
  • 15
  • 94
  • 131
2

I don't think it's because it is an implicit. I think it's just because it is a class parameter (which is private). If you drop the implicit keyword it still gives the same error. see also Do scala constructor parameters default to private val?

Community
  • 1
  • 1
Scott Shipp
  • 2,241
  • 1
  • 13
  • 20