You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@spark.apache.org by Parthus <pe...@gmail.com> on 2014/08/04 22:59:59 UTC
Create a new object by given classtag
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-unsubscribe@spark.apache.org
For additional commands, e-mail: user-help@spark.apache.org
Re: Create a new object by given classtag
Posted by Peng Wei <pe...@gmail.com>.
Hi Matei,
Thanks for your reply. Do you know how to write the code to implement what
you mentioned? I tried a lot, for example, "val obj =
ClassTag[T].runtimeClass.newInstance", but none works.
Thanks very much.
On Mon, Aug 4, 2014 at 2:42 PM, Matei Zaharia <ma...@gmail.com>
wrote:
> This is not something inherently supported by ClassTags. The best you can
> do is get the Class object for it (which is part of the ClassTag API) and
> create an instance through reflection. This will work as long as it has a
> public constructor with no parameters.
>
> Matei
>
> On August 4, 2014 at 2:00:27 PM, Parthus (peng.wei.prc@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-unsubscribe@spark.apache.org
> For additional commands, e-mail: user-help@spark.apache.org
>
>
Re: Create a new object by given classtag
Posted by Matei Zaharia <ma...@gmail.com>.
This is not something inherently supported by ClassTags. The best you can do is get the Class object for it (which is part of the ClassTag API) and create an instance through reflection. This will work as long as it has a public constructor with no parameters.
Matei
On August 4, 2014 at 2:00:27 PM, Parthus (peng.wei.prc@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-unsubscribe@spark.apache.org
For additional commands, e-mail: user-help@spark.apache.org
Re: Create a new object by given classtag
Posted by Matei Zaharia <ma...@gmail.com>.
To get the ClassTag object inside your function with the original syntax you used (T: ClassTag), you can do this:
def read[T: ClassTag](): T = {
val ct = classTag[T]
ct.runtimeClass.newInstance().asInstanceOf[T]
}
Passing the ClassTag with : ClassTag lets you have an implicit parameter that you give no name to. This is most useful if you just need it to exist to pass it to other methods that you call, but you can also pull it out.
Matei
On August 4, 2014 at 4:41:38 PM, Marcelo Vanzin (vanzin@cloudera.com) wrote:
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 <pe...@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-unsubscribe@spark.apache.org
> For additional commands, e-mail: user-help@spark.apache.org
>
--
Marcelo
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@spark.apache.org
For additional commands, e-mail: user-help@spark.apache.org
Re: Create a new object by given classtag
Posted by Marcelo Vanzin <va...@cloudera.com>.
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 <pe...@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-unsubscribe@spark.apache.org
> For additional commands, e-mail: user-help@spark.apache.org
>
--
Marcelo
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@spark.apache.org
For additional commands, e-mail: user-help@spark.apache.org