New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Secondary constructors don't allow for defaults with the same type #7870
Comments
Imported From: https://issues.scala-lang.org/browse/SI-7870?orig=1 |
@retronym said:
|
@retronym said: |
Maurício Szabo (mauricio.szabo) said: class Foo(a: Int = 0) {
def this(a: String = "0") = this(a.toInt)
} Furthermore, these snippets compiles too: class Foo(a: String = "0") { def this(p1: String, p2: Int = 0) = this(p1 + p2.toString) }
class Foo(val a: String = "0") {
def this(p1: String, p2: String = "0") = this(p1 + p2.toString)
} In these two examples above, I have constructors with default parameters, and Scala can inflect which one I want (even in the second case, when it's not really clear if I do In the ticket's example, it's clear that the secondary constructor is not "ambiguous" nor "double defined", as the first parameter it expects is a mandatory one AND has no default parameter. |
@retronym said: The constructor itself is not double defined. It is the default getter. More information on the implementation of default arguments is available here: http://docs.scala-lang.org/sips/completed/named-and-default-arguments.html |
Maurício Szabo (mauricio.szabo) said: |
@retronym said (edited on Sep 23, 2013 5:00:23 PM UTC): scala> class C { def foo(a: Int = 0, b: Int = 0) = 0; def foo(a: String = "") = 0 }
<console>:7: error: in class C, multiple overloaded alternatives of method foo define default arguments.
class C { def foo(a: Int = 0, b: Int = 0) = 0; def foo(a: String = "") = 0 }
^ This check wasn't working for constructors. My change brings them under the same rules as regular methods. scala> class C { def this(a: Int = 0, b: Int = 0) = this(); def this(a: String = "") = this() }
<console>:7: error: in class C, multiple overloaded alternatives of constructor C define default arguments.
class C { def this(a: Int = 0, b: Int = 0) = this(); def this(a: String = "") = this() }
^ |
Maurício Szabo (mauricio.szabo) said: #include <iostream>
using namespace std;
class Foo {
public:
void foo(int foo = 10, int bar = 20) {
cout << "First method\n";
}
void foo(string foo, int bar = 20) {
cout << "Second method\n";
}
};
int main(void) {
Foo f = Foo();
f.foo(10); //Prints "First method"
f.foo("String"); //Prints "Second method"
} In Scala, as I have shown in my examples, this is almost working except that for some cases it detects double definitions. Is it possible to try to correct these double definitions, in place of putting even more restrictions over constructors? (Sorry for the trouble, but working in Scala it seems cumbersome to work with constructors the way it is implemented today, comparing it with another languages, that I don't think implementing even more restrictions is quite the way to go...) |
@som-snytt said (edited on Sep 24, 2013 4:19:26 AM UTC):
I also did a double-take. The rule is to prefer the application that doesn't require default args, which seems to be a robust rule in the face of this issue. (I'm so used to Web 2.0 that I didn't notice how stale the comments page had become.) |
@retronym said: Martin recently said: "For me the conclusion is: Overloading in Scala is not a first-class feature but a "best-effort" feature. If it was not for Java interop we probably would not have it. Consequently I see no motivation to complicate the language just to make overloading work better in some cases." One pertinent difference between Scala and C++ is our support for separate compilation. Furthermore, defaults are polymorphic (not that this part applies to defaults in constructors). This means that the default getters must have a stable name. This was chosen as |
When I define secondary constructors with default parameters, if two parameters share the same type it gives me a "double definition" error. Below is the offending code:
Error:
The text was updated successfully, but these errors were encountered: