In the metrics-scala library we have the following method:
def timer(name: String, scope: String = null): Timer
I want to deprecate the scope
parameter and remove it from the next major version.
I tried this:
def timer(name: String): Timer
@deprecated(...)
def timer(name: String, scope: String): Timer
But that causes binary backward compatibility problems (see below *) already within the current major version.
I also tried this:
def timer(name: String, @deprecated(...) scope: String = null): Timer
But that gives warnings inside timer
, and not for the caller of timer
.
Did I miss something or is it really not possible to deprecate a parameter with default values?
(*) Mima report for option 1:
sbt:metrics4-scala-root> mimaReportBinaryIssues
[error] * synthetic method timer$default$2()java.lang.String in class nl.grons.metrics4.scala.MetricBuilder does not have a correspondent in current version
[error] filter with: ProblemFilters.exclude[DirectMissingMethodProblem]("nl.grons.metrics4.scala.MetricBuilder.timer$default$2")
It avoids Client confusion in passing Parameters to Method or Constructor calls. It improve the readability of the code.
def aMethod(param: String = "asdf") = { ... } If the method is called as follows, then param is given the default value "asdf": aMethod() ...
A parameter with a default value, is often known as an "optional parameter". From the example above, country is an optional parameter and "Norway" is the default value.
Scala lets you specify default values for function parameters. The argument for such a parameter can optionally be omitted from a function call, in which case the corresponding argument will be filled in with the default.
As this example shows, you can define a default value for a function parameter just like you can define a default value for an Int or String. That’s both cool, and useful.
Just as with constructor parameters, you can provide default values for method arguments. Because you have provided defaults, the consumer of your method can either supply an argument to override the default or skip the argument, letting it use its default value.
Specify the default value for parameters in the method signature. In the following code, the timeout field is assigned a default value of 5000, and the protocol field is given a default value of http:
I believe (but I don't have MiMa setup now to check) that you can use traits:
object Foo extends DeprecatedFoo {
def timer(name: String): Unit = { println("called new shiny version") }
}
trait DeprecatedFoo {
@deprecated("", "")
def timer(name: String, scope: String = null) = { println("called bad old version")}
}
Foo.timer("xx") // calls new version
Foo.timer("xx", null) // calls old version and issues a warning:
The code compiled for old version would be doing invokevirtual Foo/timer(Ljava/lang/String;Ljava/lang/String;)Z
, that would resolve to the old version too.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With