Is it possible to implement a class constructor type converter in Scala -


as example have class

 import java.sql.timestamp   class service(name: string, stime: timestamp,           etime:timestamp) 

how make accept following in implicit way, let called stringtotimestampconverter

    val s = new aservice("service1", "2015-2-15 07:15:43", "2015-2-15 10:15:43") 

time have been passed string.

how implement such converter?

you have 2 ways, first having in scope string => timestamp implicit conversion

// have in scope before instantiate object implicit def totimestamp(s: string): timestamp = timestamp.valueof(s) // convert timestamp 

the other 1 adding constructor class:

class service(name: string, stime: timestamp, etime:timestamp) {   def this(name: string, stime: string, etime: string) = {     this(name, service.totimestamp(stime), service.totimestamp(etime))   } }  object service {   def totimestamp(s: string): timestamp = timestamp.valueof(s) // convert timestamp } 

Comments

Popular posts from this blog

gridview - Yii2 DataPorivider $totalSum for a column -

java - Suppress Jboss version details from HTTP error response -

Sass watch command compiles .scss files before full sftp upload -