Jessica
Jessica

Reputation: 2435

What does "override implicit" mean?

I am looking at a piece of code that says

case class MyClass(override implicit val x : SomeClass) extends SomeOtherClass(...) {
    ...
}

What does override implicit mean in this context, and what can I do if I want to produce an instance of MyClass with explicit parameters?

Upvotes: 5

Views: 2070

Answers (1)

Łukasz
Łukasz

Reputation: 8673

This means that SomeOtherClass has a field x of type SomeClass that will be overriden by the x you pass in the constructor of MyClass.

The implicit will make the x argument for my class implicit and allow the following code:

implicit val someInt = 5
val a = new MyClass
val b = new MyClass()
val c = MyClass() // as it it a case class

If you want to produce an instance of MyClass with explicit parameters, you can pass them explicitly like this:

val a = new MyClass()(42)

(Examples assume that SomeClass is an Int, for simplicity)

To clarify: the implicit and override keywords here are unrelated.

Upvotes: 4

Related Questions