score:124
I'd like to cite Lukas Rytz (from here):
The reason is that we wanted a deterministic naming-scheme for the generated methods which return default arguments. If you write
def f(a: Int = 1)
the compiler generates
def f$default$1 = 1
If you have two overloads with defaults on the same parameter position, we would need a different naming scheme. But we want to keep the generated byte-code stable over multiple compiler runs.
A solution for future Scala version could be to incorporate type names of the non-default arguments (those at the beginning of a method, which disambiguate overloaded versions) into the naming schema, e.g. in this case:
def foo(a: String)(b: Int = 42) = a + b
def foo(a: Int) (b: Int = 42) = a + b
it would be something like:
def foo$String$default$2 = 42
def foo$Int$default$2 = 42
Someone willing to write a SIP proposal?
score:0
My understanding is that there can be name collisions in the compiled classes with default argument values. I've seen something along these lines mentioned in several threads.
The named argument spec is here: http://www.scala-lang.org/sites/default/files/sids/rytz/Mon,%202009-11-09,%2017:29/named-args.pdf
It states:
Overloading If there are multiple overloaded alternatives of a method, at most one is
allowed to specify default arguments.
So, for the time being at any rate, it's not going to work.
You could do something like what you might do in Java, eg:
def foo(a: String)(b: Int) = a + (if (b > 0) b else 42)
score:1
One of the possible scenario is
def foo(a: Int)(b: Int = 10)(c: String = "10") = a + b + c
def foo(a: Int)(b: String = "10")(c: Int = 10) = a + b + c
The compiler will be confused about which one to call. In prevention of other possible dangers, the compiler would allow at most one overloaded method has default arguments.
Just my guess:-)
score:3
Here is a generalization of @Landei answer:
What you really want:
def pretty(tree: Tree, showFields: Boolean = false): String = // ...
def pretty(tree: List[Tree], showFields: Boolean = false): String = // ...
def pretty(tree: Option[Tree], showFields: Boolean = false): String = // ...
Workarround
def pretty(input: CanPretty, showFields: Boolean = false): String = {
input match {
case TreeCanPretty(tree) => prettyTree(tree, showFields)
case ListTreeCanPretty(tree) => prettyList(tree, showFields)
case OptionTreeCanPretty(tree) => prettyOption(tree, showFields)
}
}
sealed trait CanPretty
case class TreeCanPretty(tree: Tree) extends CanPretty
case class ListTreeCanPretty(tree: List[Tree]) extends CanPretty
case class OptionTreeCanPretty(tree: Option[Tree]) extends CanPretty
import scala.language.implicitConversions
implicit def treeCanPretty(tree: Tree): CanPretty = TreeCanPretty(tree)
implicit def listTreeCanPretty(tree: List[Tree]): CanPretty = ListTreeCanPretty(tree)
implicit def optionTreeCanPretty(tree: Option[Tree]): CanPretty = OptionTreeCanPretty(tree)
private def prettyTree(tree: Tree, showFields: Boolean): String = "fun ..."
private def prettyList(tree: List[Tree], showFields: Boolean): String = "fun ..."
private def prettyOption(tree: Option[Tree], showFields: Boolean): String = "fun ..."
score:11
What worked for me is to redefine (Java-style) the overloading methods.
def foo(a: Int, b: Int) = a + b
def foo(a: Int, b: String) = a + b
def foo(a: Int) = a + "42"
def foo(a: String) = a + "42"
This ensures the compiler what resolution you want according to the present parameters.
score:13
I can't answer your question, but here is a workaround:
implicit def left2Either[A,B](a:A):Either[A,B] = Left(a)
implicit def right2Either[A,B](b:B):Either[A,B] = Right(b)
def foo(a: Either[Int, String], b: Int = 42) = a match {
case Left(i) => i + b
case Right(s) => s + b
}
If you have two very long arg lists which differ in only one arg, it might be worth the trouble...
score:70
It would be very hard to get a readable and precise spec for the interactions of overloading resolution with default arguments. Of course, for many individual cases, like the one presented here, it's easy to say what should happen. But that is not enough. We'd need a spec that decides all possible corner cases. Overloading resolution is already very hard to specify. Adding default arguments in the mix would make it harder still. That's why we have opted to separate the two.
Source: stackoverflow.com
Related Query
- Why does the Scala compiler disallow overloaded methods with default arguments?
- Why does the Scala compiler fail with missing parameter type for filter with JavaSparkContext?
- Why does the Scala compiler error with "Synthetic tree contains nonsynthetic tree"?
- Why does Scala compiler fail with "no ': _*' annotation allowed here" when Row does accept varargs?
- Why does overloading polymorphic methods with different upper bounds not compile in Scala
- Why are default arguments not allowed in a Scala section with repeated parameters?
- Why does Scala compiler for .NET ignore the meaning of val?
- Why does Scala implicit resolution fail for overloaded method with type parameter?
- Why does the Scala compiler say that copy is not a member of my case class?
- Why does Scala compiler reject function body with no leading space?
- Does the Scala compiler work with UTF-8 encoded source files?
- Why does Scala's Compiler show Scala specific features with "-print" Option?
- Why does Scala not infer the type parameters when pattern matching with @
- Why does object notation in Scala apparently eliminate the right side associativity of methods ending in ':'?
- Why does the Scala compiler differently treat these two code blocks?
- Why does the Scala compiler give "value registerKryoClasses is not a member of org.apache.spark.SparkConf" for Spark 1.4?
- Why are the scala compiler arguments passed to my program?
- Why does Scala compiler fail with "object SparkConf in package spark cannot be accessed in package org.apache.spark"?
- Why does scala return a Any(Val/Ref) when the function is obviously called with wrong types
- How to make the scala compiler find case classes used with wrong arguments
- Why does the Scala 2.11 compiler rename my private method?
- Scala file writing with Java printwriter - why does the file writer stop in this code?
- Why does the Scala compiler prefer to infer an argument of value null as Array[Char] instead of Object?
- Why does the Scala compiler recognize a generic type twice
- why does a scala class in the worksheet with same name but different case as the worksheet cause an exception?
- why scala compiler says type arguments does not conform to bounds?
- Scala covariant type declaration with an upper bound, why do methods need to repeat the upper bound exlplicitly
- Why does Mockito verifyNoMoreInteractions has problem with Scala default values
- How does the Scala compiler synthesize implicit evidence with `<:<`?
- Why can I start the Scala compiler with "java -cp scala-library.jar;. Hello World"?
More Query from same tag
- How to println from foreach in Jupyter?
- Scala: Is there a default class if no class is defined?
- What's the difference between join and cogroup in Apache Spark
- Scalatest: have a test which is valid if one of both matchers matches
- Scala implicit conversions and mkNumericOps with value classes
- How to restart a killed actor?
- How can I obtain the default value for a type in Scala?
- Implicitly aliasing and extending a scala Seq
- Making a folder/files and writing to them in scala?
- Spark GraphX - How to pass and array to to filter graph edges?
- Transform only the last element of a Scala list
- Generate derived trait with same methods but without first parameter in each method in Scala
- Testing with probabilistic failure of components in Akka (Scala)
- Manifest vs ClassManifest. What does this Scala error mean?
- Modify a method parameter value using aspectj in Scala
- Export eclipse project to build.sbt
- Removing only certain parts of scope from scala xml object
- Use look up table to assign boolean to column values in scala
- Specify negative path match for spray routes
- Scala Function Variance and Overriding
- Directed graph min-cut library
- Drop records from dataframe based on the null present for any column
- Yet another "Unable to instantiate activity ComponentInfo" with Scala
- Unable to Test POST request In Scala HTTP Akka
- inject akka actors using guice in play 2.5
- scala spark mllib fpgrowth returns different answer each time
- Akka actoreRef with macWire DI, actoreRef is not set
- Why does enablePlugins(DockerPlugin) from sbt-docker in Play project give "error: reference to DockerPlugin is ambiguous"?
- Scala Iterable to ParIterable
- Scala Akka Http - Use routes to return Future [A] as Json