scalagenericsclasstag

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 like

ConfigurationUtils.loadConfiguration[T]

the compile error is:

T is not a class

What can I do to fix this generics related issue?


Solution

  • You need to also have implicit A: Configs[T] available "when calling the first method from within the SparkBaseRunner class", and you don't. The error comes from the way this library tries to find one.

    The easy way is to make it a constructor parameter, which requires changing from a trait to an abstract class:

    abstract class SparkBaseRunner[T <: Product](implicit A: Configs[T]) extends App {
    
    // in extending class (Bar is a case class or 
    // some other type for which an implicit Configs[Bar] exists)
    class Foo extends SparkBaseRunner[Bar] { ... }
    

    If SparkBaseRunner needs to be a trait, you can add the implicit parameter to all methods which need it, or make it an abstract val and provide it manually in subclasses