开发者

Scala classOf for type parameter: how to call the function and how to restrict with upper type bounds

I'm working with JAX-RS in Scala and trying to parameterise a call to:

val jc = JAXBContext.newInstance(classOf[MyClassName])

I've been using ClassManifests as per the answer here but have a couple of things I'm still struggling with. As background, my JAX-RS representations all extend a stubbed Representation class:

class Representation {}

class ExampleRepresentation extends Representation { ... }

So far I've defined my function using a ClassManifest like so:

def get[R: ClassManifest](representation: R): String = {
  val jc = JAXBContext.newInstance(classManifest[R].erasure)
  ...
}

My first question is a bit of a silly one: how do I call this function? I can't figure out what to pass in to get() for the R type and the representation value (the accepted answer to the original question doesn't make this clear). I tried implicit typing as per paradigmatic's comment but the below generates a compile error:

get(PlatformRepresentation)

Compiling main sources... 
  not found: value P开发者_运维百科latformRepresentation

My second question is: is it possible to apply an upper type bound on the R object? In other words, I know that:

R <: Representation

Is there a way of bounding this in get()'s ClassManifest type declaration?

Many thanks!


You need to suppress the argument if you don't have any:

def get[R <: Representation: ClassManifest]: String = {
    val classManifest = implicitly[ClassManifest[R]] //to retrieve the class manifest
}

To call it:

get[PlatformRepresentation]

The type gets between square brackets.


About your second question: yes, there is a way to do that:

def get[R <: Representation: ClassManifest](representation: R): String

When you declare type parameters, you may include one lower bound with >:, one upper bound with <:, and as many context bounds (with :) and view bounds (with <%) that you need.


an example:

scala> def b[T <: String : ClassManifest] (t:T) = t + " " + classManifest[T].era
sure;
b: [T <: String](t: T)(implicit evidence$1: ClassManifest[T])java.lang.String

scala> b("hello")
res2: java.lang.String = hello class java.lang.String

EDIT @paradigmatic is right, in your case it should be

scala> def c[T <: String : ClassManifest] = classManifest[T].erasure;
c: [T <: String](implicit evidence$1: ClassManifest[T])java.lang.Class[_]

scala> c[String];
res4: java.lang.Class[_] = class java.lang.String
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜