lambdas
lambdas

Reputation: 4088

How to call `apply` on a superclass in Scala?

My class inherits from some base class, and implements apply method with exactly the same signature as the base's one. I want to call base's apply method from my class. When I try following:

class MyClass extends BaseClass {
  def apply(k: String, v: String) = {
    super.apply(k, v)
    ...
  }
  ...
}

I got value apply is not a member of BaseClass... compile error. How should I call base's apply method from the child class?

Also, why it is possible to override apply method without an override keyword?

EDIT: Actual code:

class OAuthParamsBuilder(helper: OAuthParamsHelper)
extends KeyValueHandler {

  def apply(k: String, v: String): Unit = {
    ...
  }
}

class OAuthInitSupportBuilder
extends OAuthParamsBuilder(StandardOAuthParamsHelper) {

  /*override*/ def apply(k: String, v: String): Unit = {
    super.apply(k, v)
    ...
  }
...
}

EDIT: I've noticed that KeyValueHandler is a trait, this may be an issue.

trait KeyValueHandler extends ((String, String) => Unit)

Upvotes: 0

Views: 1176

Answers (1)

Daniel C. Sobral
Daniel C. Sobral

Reputation: 297305

You are not helping us help you, but I suspect this is the true definition of apply on the base class:

def apply(kv: (String, String)) = ???

EDIT

The code you pasted is not enough, as the problem is not reproducible with it:

trait OAuthParamsHelper
trait KeyValueHandler
class OAuthParamsBuilder(helper: OAuthParamsHelper) extends KeyValueHandler {
  def apply(k: String, v: String): Unit = ???
}
object StandardOAuthParamsHelper extends OAuthParamsHelper
class OAuthInitSupportBuilder extends OAuthParamsBuilder(StandardOAuthParamsHelper) {
  override def apply(k: String, v: String): Unit = {
    super.apply(k, v)
    ???
  }
}

Upvotes: 1

Related Questions