如何在不修改此特定 class 的情况下扩展具有功能的 Scala class?

How to extend a Scala class with functionality without modifying this particular class?

假设我想封装 class 的功能,这意味着我想在客户端和这个特定的 class 之间引入代理。比如Play的Resultclass有如下方法:

  /**
   * Adds headers to this result.
   *
   * For example:
   * {{{
   * Ok("Hello world").withHeaders(ETAG -> "0")
   * }}}
   *
   * @param headers the headers to add to this result.
   * @return the new result
   */
  def withHeaders(headers: (String, String)*): Result = {
    copy(header = header.copy(headers = header.headers ++ headers))
  }

假设我不想直接使用此方法,而是想使用代理。因此,我不想调用 Redirect(controllers.app.routes.Application.index).withHeaders(<XYZ>),而是调用我自己的方法,该方法随后调用 .withHeaders(<XYZ>):

Redirect(controllers.app.routes.Application.index).customWithHeaders(<XYZ>)

如何实施?这可以通过实现将由控制器扩展的特征来完成吗?

import play.api.mvc.Result

implicit class ResultUtils (result: Result) {
    def withCustomHeaders(pairs: (String, String)*): Result = result.withHeaders(pairs: _*)
}

在范围内使用 ResultUtils,您现在可以 Redirect(something).withCustomHeaders

包对象

package object common {
    implicit class ResultUtils (result: Result) {
      def withCustomHeaders(pairs: (String, String)*): Result = result.withHeaders(pairs: _*)
    }
}

import common._ //import common and get this class in scope

特质

trait Implicits {
  implicit class ResultUtils (result: Result) {
      def withCustomHeaders(pairs: (String, String)*): Result = result.withHeaders(pairs: _*)
    }
}

class ApplicationController @Inject() () extends Controller extends Something with Implicits {
  def fooAction = Action { Ok("bar").withCustomHeaders("foo" -> "bar") }
}