【问题标题】:How to eliminate vars in a Scala class?如何消除 Scala 类中的变量?
【发布时间】:2015-07-04 06:26:12
【问题描述】:

我根据对应的Java类写了如下Scala类: 结果不好。它看起来仍然像 Java,充满了变量,很长,而且在我看来不是惯用的 Scala。

我希望缩小这段代码,消除变量和@BeanHeader 的东西。

这是我的代码:

    import scala.collection.immutable.Map 

     class ReplyEmail {

     private val to: List[String] = List()   
     private val toname: List[String] = List()
     private var cc: ArrayList[String] = new ArrayList[String]()

    @BeanProperty
    var from: String = _

    private var fromname: String = _

    private var replyto: String = _

    @BeanProperty
    var subject: String = _

    @BeanProperty
    var text: String = _

    private var contents: Map[String, String] = new scala.collection.immutable.HashMap[String, String]()

    @BeanProperty
    var headers: Map[String, String] = new scala.collection.immutable.HashMap[String, String]()

    def addTo(to: String): ReplyEmail = {
      this.to.add(to)
      this
    }

    def addTo(tos: Array[String]): ReplyEmail = {
      this.to.addAll(Arrays.asList(tos:_*))
      this
    }

    def addTo(to: String, name: String): ReplyEmail = {
      this.addTo(to)
      this.addToName(name)
    }

    def setTo(tos: Array[String]): ReplyEmail = {
      this.to = new ArrayList[String](Arrays.asList(tos:_*))
      this
    }

    def getTos(): Array[String] = {
      this.to.toArray(Array.ofDim[String](this.to.size))
    }

    def getContentIds(): Map[_,_] = this.contents

    def addHeader(key: String, `val`: String): ReplyEmail = {
      this.headers + (key -> `val`)
      this
    }

     def getSMTPAPI(): MyExperimentalApi = new MyExperimentalApi
      }

   }

=----------

感谢您为实现这一目标提供的任何帮助。 更新代码

我对代码做了一些小改动,比如引入 Option[String] 而不是 String

case class ReplyEmail(
  to: List[String] = Nil,
  toNames: List[String] = Nil,
  cc: List[String],
  from: String,
  fromName: String,
  replyTo: String,
  subject: String,
  text: String,
  contents: Map[String, String] = Map.empty,
  headers: Map[String, String] = Map.empty) {
  def withTo(to: String): ReplyEmail = copy(to = this.to :+ to)
  def withTo(tos: List[String]): ReplyEmail = copy(to = this.to ++ to)
  def withTo(to: Option[String], name: Option[String]) = copy(to = this.to :+ to, toNames = toNames :+ name)

  def setTo(tos: List[String]): ReplyEmail = copy()
  def withHeader(key: String, value: String) = copy(headers = headers + (key  -> value))
  def smtpAPI = new MyExperimentalApi

}


现在,我面临的唯一问题是这一行: 错误是:类型不匹配:找到:List[java.io.Serializable] required: List[String]

def withTo(to: Option[String], name: Option[String]) = copy(to = this.to :+ to, toNames = toNames :+ name)

【问题讨论】:

  • 关于您的编辑:您正在致电(List[String]) :+ (Option[String])。你会得到List[Serializable],因为这就是StringOption[String] 的共同点。如果已定义,您可能希望将项目添加到列表中,否则保持列表不变。您可以这样做:to.foldLeft(this.to) { _ :+ _ }
  • 您也可以将您的 to/toNames 和 from/fromName 关系重构为case class EmailAddress(address: String, name: Option[String])
  • 好的,根据你的建议,我就是这样做的: def withTo(to: Option[String], name: Option[String]) = copy(to.foldLeft(this.to ){_ :+ }, to.foldLeft(this.toNames){ :+ _}) 看起来对吗?
  • 我根据你所说的更新了我的代码:“ def withTo(to: Option[String], name: Option[String]) = copy(toList = to.foldLeft(this.toList ){ _ :+ }, toNames = to.foldLeft(this.toNames){ :+ _}) 对吗?

标签: scala


【解决方案1】:

只需将其设为案例类即可。

case class ReplyEmail(
  to: List[String] = Nil,
  toNames: List[String] = Nil,
  cc: List[String],
  from: String,
  fromName: String,
  replyTo: String,
  subject: String,
  text: String,
  contents: Map[String, String] = Map.empty,
  headers: Map[String, String] = Map.empty) {

  def withTo(to: String) = copy(to = this.to :+ to)

  def withTo(to: List[String] = copy(to = this.to ++ to)

  def withTo(to: String, name: String) = copy(to = this.to :+ to, toNames = toNames :+ name)

  def withHeader(key: String, value: String) = copy(headers = headers + (key -> value))

  def smtpAPI = new MyExperimentalApi

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-26
    • 2016-05-11
    • 1970-01-01
    • 2013-09-26
    • 2016-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多