简体   繁体   English

如何定义此方法的结果类型?

[英]How define the result type of this method?

How do I define the method return type in the following case: 在以下情况中,如何定义方法返回类型:

working code 工作代码

def deleteInstance(model: String, uid: Long) =  model match {
    case "menu" => Model.all(classOf[Menu]).filter("uid", uid).get().delete()
    case "articles" => Model.all(classOf[Articles]).filter("uid", uid).get().delete()
    case "news" => Model.all(classOf[News]).filter("uid", uid).get().delete()
    case "image" =>Model.all(classOf[Image]).filter("uid", uid).get().delete()
    case "files" =>Model.all(classOf[Files]).filter("uid", uid).get().delete()
    case _ => false
  }

non-working code: 非工作代码:

class ModelManager{
  def getModel(model: String) = {
    model match{
      case "menu" => classOf[Menu]
      case "articles" => classOf[Articles]
      case _ => false
    }

  def deleteInstance(model:String, uid: Long) = {
    Model.all(getModel(model)).filter("uid", uid).get().delete()
  }    
 }
} 

Error raised is: 引发的错误是:

recursive method getModel needs result type 递归方法getModel需要结果类型

It looks like you need an Option : 看起来你需要一个选项

class ModelManager{
   def getModel(model: String) = model match {
      case "menu" => Some(classOf[Menu])
      case "articles" => Some(classOf[Articles])
      case _ => None
   }

   def deleteInstance(model:String, uid: Long) = 
      getModel(model) map { m => 
         Model.all(m).filter("uid", uid).get().delete()
      } getOrElse false
}

You can think of an Option as a container that can hold at most one element. 您可以将Option视为最多可容纳一个元素的容器。 The Option that holds an element x is Some(x) . 包含元素x的选项是Some(x) The empty Option is None . 空选项为None Option has several useful methods, including the map and getOrElse methods used above. Option有几种有用的方法,包括上面使用的mapgetOrElse方法。

The map method applies a function to each element of the "container". map方法将函数应用于“容器”的每个元素。 Of course, if the container is None , it does nothing (except perhaps to change the static type of the Option). 当然,如果容器是None ,它什么也不做(除了可能改变Option的静态类型)。 In your case (assuming delete returns a Boolean), the map method will change the Option[Class] into an Option[Boolean]. 在您的情况下(假设delete返回一个布尔值),map方法会将Option [Class]更改为Option [Boolean]。

The getOrElse method returns the element of the option, if there is one, and otherwise returns a default value ( false in this case). getOrElse方法返回选项的元素(如果有),否则返回默认值(在本例中为false )。

Note that you can also simplify your implementation by using the condOpt method defined in PartialFunction : 请注意,您还可以通过使用简化您的实现condOpt中定义的方法PartialFunction

class ModelManager{
   def getModel(model: String) = condOpt(model) {
      case "menu" => classOf[Menu]
      case "articles" => classOf[Articles]
   }

   def deleteInstance(model:String, uid: Long) = 
      getModel(model) map { m => 
         Model.all(m).filter("uid", uid).get().delete()
      } getOrElse false
}

It looks like getModel will return a Class sometimes, a Boolean others. 看起来getModel有时候会返回一个Class,而另一个则是Boolean。 In Scala, this would typically be modeled using the Either class: 在Scala中,通常使用Either类对其进行建模:

def getModel(model: String) = {
    model match{
      case "menu" => Left(classOf[Menu])
      case "articles" => Left(classOf[Articles])
      case _ => Right(false)
    }

Left and Right represent the two possible choices of an Either. 左和右代表Either的两种可能选择。 Callers of this method will need to inspect the return value (probably by using pattern matching as well) to decide if the method returned a Class or Boolean. 此方法的调用者需要检查返回值(可能还通过使用模式匹配)来确定该方法是返回Class还是Boolean。

It seems you didn't close with parens in the right place. 看来你没有在正确的地方与parens关闭。 Did you mean this? 你的意思是?

class ModelManager{
  def getModel(model: String) = {
    model match{
      // snip
    }
  } // end method here

  def deleteInstance(model:String, uid: Long) = {
    Model.all(getModel(model)).filter("uid", uid).get().delete()
  }    
} 

It does not look like you're trying to define a recursive method... Then you're likely to have other issues to resolve as you need a method that returns Class[_] not a combination of Boolean and Class[_] (which would be Any ). 它看起来并不像你试图定义一个递归方法......然后你可能有其他问题需要解决,因为你需要一个返回Class [_]而不是BooleanClass[_]的组合的方法(这将是Any )。 So may be this would work better? 那可能会更好吗?

def getModel(model: String): Class[_] = {
  model match{
    case "menu" => classOf[Menu]
    case "articles" => classOf[Articles]
} // end method here

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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