【问题标题】:Scala override errorScala覆盖错误
【发布时间】:2014-06-03 09:43:55
【问题描述】:

我正在使用 Play 框架 2.2.x 和 SecureSocial 并且为了测试,我正在尝试覆盖超类中的方法。实际上,我在使用来自安全社交的测试帮助程序类时遇到了编译错误,我无法弄清楚,因为我对 Scala 并不那么熟悉。

我不明白为什么我的 ide 方法被正确覆盖但是当我编译时出现错误。我知道 scala 与 ides 的集成度不是很好,但我看不出为什么覆盖不正确。

/**
 *  This is the secure social class and method I am trying to override. 
    I have left the other methods out for clarity. The problem method is the doAuth method
 **/

包securesocial.core

import providers.utils.RoutesHelper
import play.api.mvc.{SimpleResult, AnyContent, Request}
import play.api.{Play, Application, Plugin}
import concurrent.{Await, Future}
import play.api.libs.ws.Response

abstract class IdentityProvider(application: Application) extends Plugin with Registrable {
 /**
   * Subclasses need to implement the authentication logic. This method needs to return
   * a User object that then gets passed to the fillProfile method
   *
   * @param request
   * @return Either a Result or a User
   * This is the method I am having trouble trying to override
   */
  def doAuth()(implicit request: Request[AnyContent]):Either[SimpleResult, SocialUser]
}

这是我的类和 doAuth() 覆盖 打包测试包

import play.api.Logger
import securesocial.core._
import play.api.mvc.{Result, Request}
import securesocial.core.IdentityId

class AlwaysValidIdentityProvider(app:play.api.Application) extends IdentityProvider(app){
  val logger = Logger("securesocial.stubs.AlwaysValidIdentityProvider")
  def authMethod: AuthenticationMethod = AuthenticationMethod("naive")


  override def doAuth()(implicit request: Request[play.api.mvc.AnyContent]): Either[Result, SocialUser] ={
    val userId = request.body.toString
    val r =Right(SocialUserGenerator.socialUserGen(IdentityId(userId, id), authMethod).sample.get)
    r
  }


  def fillProfile(user: SocialUser): SocialUser = {
    user
  }

  def id: String = "naive"
}

我得到的错误是:如您所见,它认为我没有覆盖任何内容。

[error] /Users/zola/Development/play/receipt-manager/rm-play/test/testkit/AlwaysValidIdentityProvider.scala:8: class AlwaysValidIdentityProvider needs to be abstract, since method doAuth in class IdentityProvider of type [A]()(implicit request: play.api.mvc.Request[A])Either[play.api.mvc.Result,securesocial.core.SocialUser] is not defined
[error] class AlwaysValidIdentityProvider(app:play.api.Application) extends IdentityProvider(app){
[error]       ^
[error] /Users/zola/Development/play/receipt-manager/rm-play/test/testkit/AlwaysValidIdentityProvider.scala:13: method doAuth overrides nothing.
[error] Note: the super classes of class AlwaysValidIdentityProvider contain the following, non final members named doAuth:
[error] def doAuth[A]()(implicit request: play.api.mvc.Request[A]): Either[play.api.mvc.Result,securesocial.core.SocialUser]
[error]   override def doAuth()(implicit request: Request[play.api.mvc.AnyContent]): Either[Result, SocialUser] ={
[error]                ^
[error] two errors found

【问题讨论】:

  • 不是因为返回类型不同吗?例如。 ResultSimpleResult.

标签: scala playframework securesocial


【解决方案1】:

那是因为您要覆盖的方法尚未定义。你在抽象类中有一个签名,就是这样。

试试这个:

def doAuth()(implicit request: Request[play.api.mvc.AnyContent]): Either[Result, SocialUser] ={
    val userId = request.body.toString
    val r =Right(SocialUserGenerator.socialUserGen(IdentityId(userId, id), authMethod).sample.get)
    r
}

【讨论】:

  • 所以抽象类中的那个不在我的控制之下它属于SecureSocial框架所以我不能改变它
  • 实际上,抽象类中的那个只是显示参数和返回类型的签名。基本上,通过从抽象类继承,您必须提供自己的实现。您可以稍后实例化将使用您的实现的类的对象。当您已经在父抽象类中定义了实现时,使用覆盖。
  • 谢谢彼得,我完全掌握了覆盖的概念,当我尝试覆盖这个时,我只是不明白为什么它会出错。因此,即使有您的建议,我也会在 ide 中收到一个错误,说它没有覆盖超类方法,如果我在 ide 中自动实现它在 ide 中很好,但是当我编译时,我得到了上述错误。
【解决方案2】:

为了解决这个问题,我查看了 SecureSocial 中的其他类,他们实现该方法的方式是

    def doAuth[A]()(implicit request: Request[A]): Either[Result, SocialUser] = {
     val userId = request.body.toString
     val r =Right(SocialUserGenerator.socialUserGen(IdentityId(userId, id), authMethod).sample.get)
     r
 }

【讨论】:

    猜你喜欢
    • 2015-03-06
    • 1970-01-01
    • 2013-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多