what considered correct way create polymorphic constructors in scala? on 1 hand, can use companion object:
class userrecord (override val userid: string, override val rep: int) extends record object userrecord extends record { def apply(line: string) = { val elem = xml.loadstring(line) val userid = elem \ "@id" text val rep = (elem \ "@reputation" text).toint val out = new userrecord(userid, rep) out } }
but loses encapsulation, , call object without new
obscures fact non-singleton object being created. on other hand, can use sequence of alternate constructors, bit clunky:
class userrecord (override val userid: string, override val rep: int) extends record { def this(t: tuple2[string, int]) = this(t._1, t._2) def this(line: string) = this({ val elem = xml.loadstring(line) val userid = elem \ "@id" text val rep = (elem \ "@reputation" text).toint val out = (userid, rep) out }) }
i've looked ways avoid using first alternate constructor using .tupled
haven't cracked nut.
Comments
Post a Comment