2

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?

Georg Heiler
  • 16,916
  • 36
  • 162
  • 292
  • `T` is just a placeholder name, when you call the method you need to specify a concrete type – puhlen Dec 19 '17 at 16:38
  • You mean I need to specify a concrete name of a class like `case class Foo()`, i.e. `Foo`? – Georg Heiler Dec 19 '17 at 16:39
  • Yes, any class that extends `Product` will do. `T <: Product` means that whatever type you choose, it must extends `Product. case classes and tuples all extend `Product` implicitly. – puhlen Dec 19 '17 at 16:40
  • Is there no way I can simply specify a generic type i.e. `T` here? And have the concrete class be specified in the subclass of the trait `SparkBaseRunner[T <: Product]`. – Georg Heiler Dec 19 '17 at 16:43
  • 1
    You should be able to specify the type T actually, so I'm not sure where that error is coming from. Do you have a complete example you could post? The implicit Configs parameter to `loadConfiguration` could be a problem though. – puhlen Dec 19 '17 at 17:06

1 Answers1

3

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

Alexey Romanov
  • 167,066
  • 35
  • 309
  • 487