Hello,

Try something like this:

scala> def newFoo[T]()(implicit ct: ClassTag[T]): T =
ct.runtimeClass.newInstance().asInstanceOf[T]
newFoo: [T]()(implicit ct: scala.reflect.ClassTag[T])T

scala> newFoo[String]()
res2: String = ""

scala> newFoo[java.util.ArrayList[String]]()
res5: java.util.ArrayList[String] = []



On Mon, Aug 4, 2014 at 1:59 PM, Parthus <peng.wei....@gmail.com> wrote:
> Hi there,
>
> I was wondering if somebody could tell me how to create an object with given
> classtag so as to make the function below work. The only thing to do is just
> to write one line to create an object of Class T. I tried new T but it does
> not work. Would it possible to give me one scala line to finish it? Thanks
> very much
>
> def read[T: ClassTag](path: String): T = {
>
>     val obj = new T ??? // This does not work
>
>     obj.load(path)
>
>     obj
>
> }
>
>
>
> --
> View this message in context: 
> http://apache-spark-user-list.1001560.n3.nabble.com/Create-a-new-object-by-given-classtag-tp11368.html
> Sent from the Apache Spark User List mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscr...@spark.apache.org
> For additional commands, e-mail: user-h...@spark.apache.org
>



-- 
Marcelo

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscr...@spark.apache.org
For additional commands, e-mail: user-h...@spark.apache.org

Reply via email to