scala - 如何消除 Scala 类中的变量?

标签 scala

我基于相应的 Java 类编写了以下 Scala 类: 结果并不好。它看起来仍然像 Java,充满了变量,很长,而且在我看来不是惯用的 Scala。

我希望缩小这段代码,消除 vars 和 @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


现在,我面临的唯一问题是这一行: 错误是:type mismatch: found: List[java.io.Serializable] required: List[String]

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

最佳答案

就让它成为一个案例类。

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

}

关于scala - 如何消除 Scala 类中的变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29867993/

相关文章:

scala - 如何使用 scala 类成员名称作为变量

java - Scala 无法解析继承的 Java 接口(interface)常量成员

Scala:为什么修复类型成员会创建 Java 子类?

security - 在 Play 2.1 中使用 ajax 进行身份验证

java - 可选化 Java getter

scala - 如何在 Scala 2.8 中实现集合?

sockets - 随后调用Actor Freeze计划

scala - 后台作业服务,如何确保每个集群的作业只定期运行一次

scala - 是否需要为 Scala 2.10 上导出的宏库导出 Quasiquotes 依赖项?

c# - Scala:Java、C#、Scala 和 C++ 中的高级类、开放类型和通配符泛型