简体   繁体   中英

Extend object inside of trait in Scala

I've the following code:

trait AcceptExtractors {

  /**
   * Common extractors to check if a request accepts JSON, Html, etc.
   * Example of use:
   * {{{
   * request match {
   *   case Accepts.Json() => Ok(toJson(value))
   *   case _ => Ok(views.html.show(value))
   * }
   * }}}
   */
  object Accepts {
    import play.api.http.MimeTypes
    val Json = Accepting(MimeTypes.JSON)
    val Html = Accepting(MimeTypes.HTML)
    val Xml = Accepting(MimeTypes.XML)
    val JavaScript = Accepting(MimeTypes.JAVASCRIPT)
  }

}

Is there any way to extend the Accepts object?

Thank you!

Nope.

Object s are single values. If they could be extended, they would not be singletons (*). The class generated to represent them is final, so even if you knew its name (not hard to find out), you could not extend it.

(*) Object s are only truly singletons when defined at global scope or nested strictly within other object s leading back to a global one.

不可以,但是您可以使“接受”特征可以扩展。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM