Questions tagged [classtag]
22 questions
3
votes
1 answer
Reading type parameter's ClassType in trait
Let's say I have a trait Foo, with type parameter T, and I need a ClassTag of T to be used in Foo:
trait Foo[T] {
implicit def ct: ClassTag[T]
}
I want to achieve that without implementing ct in every Foo implementation. e.g. I want to avoid…

Gonzalo Guglielmo
- 629
- 3
- 10
3
votes
1 answer
Pass a function with any case class return type as parameter
This might be a silly question but I've been struggling for quite some time. It is indeed similar to this question but I wasn't able to apply it in my code (duo to patterns or being a function).
I want to pass a flatMap (or map) transform function…

leovrf
- 605
- 1
- 6
- 17
3
votes
2 answers
Passing case class into function arguments
sorry for asking a simple question. I want to pass a case class to a function argument and I want to use it further inside the function. Till now I have tried this with TypeTag and ClassTag but for some reason, I am unable to properly use it or may…

Sunil Kumar
- 390
- 1
- 7
- 25
3
votes
1 answer
ficus configuration load generic
Loading a ficus configuration like
loadConfiguration[T <: Product](): T = {
import net.ceedubs.ficus.readers.ArbitraryTypeReader._
import net.ceedubs.ficus.Ficus._
val config: Config = ConfigFactory.load()
config.as[T]
fails with:
Cannot generate a…

Georg Heiler
- 16,916
- 36
- 162
- 292
2
votes
1 answer
scala T is not a class
I have a method like:
def loadConfiguration[T <: Product](implicit A: Configs[T]): T = {
and a class
trait SparkBaseRunner[T <: Product] extends App {
when calling the first method from within the SparkBaseRunner class…

Georg Heiler
- 16,916
- 36
- 162
- 292
2
votes
1 answer
Get ClassTag implicitly for abstract type
I'm trying to get ClassTag implicitly. I have the following trait:
trait Tst{ self =>
type T
def getCls = getCls0[T] //no classtag
private def getCls0[T](implicit ev: ClassTag[T]) = ev.runtimeClass.asInstanceOf[Class[T]]
}
class Tsss extends…

St.Antario
- 26,175
- 41
- 130
- 318
1
vote
1 answer
Scala Any => native format
I have a map of type Map[_, Any], and I want to extract the values in their native format (without resorting to .asInstanceOf[_]).
Something like this...
val m: Map[String, Any] = Map("i" -> 1, "s" -> "test")
val i: Option[Int] = m.get("i")
val s:…

kmh
- 1,516
- 17
- 33
1
vote
1 answer
How to manipulate a map using a parameterized class as the key
I am trying to define a map with a parameterized class as its key. But when I try to add to it I get a compiler error:
trait MyTrait[T <: MyTrait[T]]
case class A(i: Int) extends MyTrait[A]
case class B(str: String) extends MyTrait[B]
var map =…

user79074
- 4,937
- 5
- 29
- 57
1
vote
1 answer
Prove that a runtimeClass satisfies a type Bound in Scala
I have a method that writes one of my classes Foo, which is defined as Thrift, in Parquet form.
import Foo
import org.apache.spark.rdd.RDD
import org.apache.thrift.TBase
import org.apache.hadoop.mapreduce.Job
import…

Michael K
- 2,196
- 6
- 34
- 52
1
vote
3 answers
type is erased from implicit class
I'm having a implicit class to add a certain function for a case class, for example:
case class TestClass(name:String)
implicit class ChangeProduct[S <: Seq[T], T <: Product](seq: Option[Seq[T]]) {
def convert(expr: T => T): Option[Seq[T]] =…

user3593261
- 560
- 4
- 17
1
vote
1 answer
Scala: Type annotation of parameter resets ClassTag to Option and spoils type matching
I have a trait with a type parameter. Inside this trait I want to define a method which checks if an object matches the type parameter. So far, this works, as I want it to work:
import scala.reflect.ClassTag
trait MyTrait[A <: AnyVal]{
def…

Niclas von Caprivi
- 709
- 1
- 7
- 12
1
vote
1 answer
How are ClassTags made available in Scala?
With
def fn[T: ClassTag](...)
there needs to be an implicit ClassTag[T] available. How are these made available? Is there a list somewhere of ClassTags? I see an implicit val c: ClassTag[Any] wouldn't cut it as ClassTag is nonvariant. And how are…

joel
- 6,359
- 2
- 30
- 55
1
vote
1 answer
Scala Map to Case Class conversion
I'm trying to convert a Scala Map[String, Any] to a case class using Scala reflection (Scala 2.11) as follows -
val m = Map("name" -> "ABC", "age" -> 7, "gender" -> "male")
case class someCC(name: String, age: Int, gender: String)
import…

Yash
- 1,080
- 2
- 13
- 24
0
votes
1 answer
ClassTag in Scala
In Scala, during type erasure, the generic variable is replaced by 'Object' type wherever the generic variable appears in type position.
E.G: val x T; --> is replaced by val x Object;
Due to this, the details of exact type which is passed, will…

user3103957
- 636
- 4
- 16
0
votes
1 answer
Scala - How to Convert Generic List To Array and avoid "No ClassTag available for T"
I am working on what should be a trivial piece of code. I want to take a List, and convert it to an equivalent Array:
import scala.collection.JavaConverters
object Help extends App {
def f1[T](lst: List[T]) = {
lst.toArray
}
val x =…

finite_diffidence
- 893
- 2
- 11
- 20