-
Notifications
You must be signed in to change notification settings - Fork 1.1k
new-less object construction does not work on secondary constuctors #7217
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
Comments
Hmm..this works in the repl...but my program is not working when I do this. It may be due to other factors. I'll close until I can sort this out. |
Well, I just ran into this again in another code area. I'll try to identify the conditions under which "new" is required when creating an instance. It appears to be happening inside single statement block. |
@aappddeevv Thanks for reporting. I'm closing this now as the given code snippet compiles without problem. When you get a snippet that does not compile, please feel free to update the issue and re-open. |
Wait, does this mean that there was an issue? I just did this on 0.18 cala> case class Foo(msg1: String, msg2: String) {
| def this(msg1: String) = this(msg1, "blah")
| }
// defined case class Foo
scala> Foo("blah")
1 |Foo("blah")
|^^^^^^^^^^^
|missing argument for parameter msg2 of method apply: (msg1: String, msg2: String): Foo
scala> Foo("blah", "hah")
val res4: Foo = Foo(blah,hah)
scala> new Foo("blah")
val res5: Foo = Foo(blah,blah) |
Thanks @aappddeevv . It seems to be overloading resolution problem related to case class Foo(msg1: String, msg2: String) {
def this(msg1: String) = this(msg1, "blah")
}
val foo: Foo = Foo("blah") // error if we have `case` for `class Foo` |
As discussed in an internal meeting, the proper way to add another constructor for case classes is to introduce an case class Foo(msg1: String, msg2: String)
object Foo {
def apply(msg1: String) = new Foo(msg1, "blah")
}
val foo1: Foo = Foo("blah")
val foo2: Foo = Foo("blah", "blah") The rule is that, if a companion object contains |
I think as long as the docs reflect this, I don't believe they did, then it is the intended behavior. |
@aappddeevv Thanks for pointing out, the specification [1] says the following:
The current behavior is consistent with the 2nd rule. [1] http://dotty.epfl.ch/docs/reference/other-new-features/creator-applications.html |
That works! |
I was calling the new-less object secondary constructor but it would not work without using new.
Boiled down example from my code:
I needed to add
new Foo("blah")
to gety
to compile.The text was updated successfully, but these errors were encountered: