why this is fatal with using scalaCoptions “-Xfatal-warnings” option
This option makes warnings treated as fatal errors. The error you got is usually a warning and thanks to this config it was treated as an error, thus failing the build.
It’s a good practice to have this option to catch case like the one you found which would silently not work as intended at runtime otherwise.
is there more idiomatic approach for addressing those validations
First, you need to understand the issue raised by the warning/error here: generic types in the method are not preserved in Java (thus Scala) at runtime, they are only available at compile time.
In Scala however, there is a feature called
ClassTag that allow to retain the generic type at runtime as well.
You can use it like this:
def validateResults[T : ClassTag, A](payloadValue:A, callerName: String, defaultReturn: T):T = // Or with the implicit syntax: def validateResults[T, A](payloadValue:A, callerName: String, defaultReturn: T)(implicit ev: ClassTag[T]):T =
And that’s it, Scala compiler will “inject” the type
T so that it is available at runtime and can be used for type checking. Nothing to change in the method body.