How can I eliminate vars in a Scala class?

I wrote the following Scala class based on the corresponding Java class: the result is not appropriate. It still looks Java-like, is replete with vars, is very long, and not idiomatic Scala in my opinion.

I want to compress this piece of code, eliminate the vars and @BeanHeader stuff.

Here is my code:

    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
      }

   }

      

= ---------------------

I appreciate any help in achieving this goal. Updated code

I made some small changes to the code, for example adding the [String] parameter instead of the 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

      

}


Now the only problem I am running into is on this line: Error: 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)

      

+3


source to share


1 answer


Just create a case class.



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

}

      

+6


source







All Articles