How can I pass a variable list of parameters and types to a function?
I.e. the depicted approach which is using pattern matching seems a bit clumsy.
In an trait the function foo is defined. However in concrete implementations a different subtype (with additional fields should be used). Is there a cleaner approach than using pattern matching?
def foo[T <: MyBaseConfiguration](config: T) = {
println("do smething")
println(config.configValue)
}
override def foo[T <: MyBaseConfiguration](config: T) = {
config match {
case c: MyOtherConfiguration => {
println("do smething else")
println(c.configValue)
println(c.otherValue)
}
}
}
trait MyBaseConfiguration {
def configValue: String
}
class MyOtherConfiguration extends MyBaseConfiguration {
val otherValue = 1234
override def configValue = "abcd"
}
edit
Basicylly, I just want to say * there is a function f with a defined return value TReturn but be agnostic to input parameters.
Still, I need to be able to use / access them during the execution of f.
def foo(x: Any*): TReturnwill allow you to "pass a variable list of parameters and types to a function" but I suspect that is not what you want. I think you need to explain how you want to use this function that you are describing.otherValueandconfigValueand still keep type safety.otherFoonotoverride foo? As I said, I think you need to explain your question more clearly so we can work out what you actually want to do. I'm sure we can help you, but you need to be clear on what it actually is that you want.