Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to deprecate a parameter with default value in Scala?

Tags:

scala

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")
like image 720
Erik van Oosten Avatar asked Nov 25 '18 09:11

Erik van Oosten


People also ask

What do default values and named parameters help you avoid in your code in Scala?

It avoids Client confusion in passing Parameters to Method or Constructor calls. It improve the readability of the code.

What is the default value for string in Scala?

def aMethod(param: String = "asdf") = { ... } If the method is called as follows, then param is given the default value "asdf": aMethod() ...

What are the parameters that are default values?

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.

What are default values in Scala functions?

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.

Can I define a default value for a function parameter?

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.

How do I set default values for method arguments?

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.

What is the default value for parameters in the method signature?

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:


1 Answers

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.

like image 177
Oleg Pyzhcov Avatar answered Oct 14 '22 22:10

Oleg Pyzhcov