How to eliminate vars in a Scala class? -


i have written following scala class based on corresponding java class: result not good. still looks java-like, replete vars, long, , not idiomatic scala in opinion.

i looking shrink piece of code, eliminate vars , @beanheader stuff.

here 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)           }      def addto(tos: array[string]): replyemail = {       this.to.addall(arrays.aslist(tos:_*))           }      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:_*))           }      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`)           }       def getsmtpapi(): myexperimentalapi = new myexperimentalapi       }     } 

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

i appreciate in accomplishing goal. updated code

i made small changes code, introducing option[string] instead of 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, problem face in line: error is: 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) 

just make 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  } 

Comments

Popular posts from this blog

jquery - How do you format the date used in the popover widget title of FullCalendar? -

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -