What is the meaning of a generic type like "C: ClassTag : Manifest"?

70 Views Asked by At

I found some snippets as follow:

import org.json4s.DefaultFormats
import org.json4s.jackson.JsonMethods._

import scala.io.Source
import scala.reflect.ClassTag

class ConfigLoader[C: ClassTag : Manifest](filePath: String) {

  def loadFromFile(): C = {
    implicit val formats = DefaultFormats
    parse(Source.fromFile(filePath).mkString, false).extract[C]
  }

}

What's the meaning of generic type like C: ClassTag : Manifest?

1

There are 1 best solutions below

0
On

The syntax C: ClassTag is a shorthand for (implicit ct: ClassTag[C]).

I.e. the full form of the class constructor is

class ConfigLoader[C](filePath: String)(implicit ct: ClassTag[C], m: Manifest[C]) { ... }

In practical terms, it means that inside the definition of ConfigLoader, instances of ClassTag[C] and Manifest[C] are required, and thus they have to be provided as arguments to the constructor.