You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
classPerson(
valfirstName:String,
vallastName:String,
valbirthYear:Int=-1,
valaddress:String=""
):// Works if remove this constructordefthis() =this("John", "Doe")
@main
defmain() =valp1=Person("John", "Doe")
valp2=Person("Josh", "Doe", 1912, "Main Street")
Running with scala-cli run -S 3.nightly applyDefaults2.scala, we get:
[error] ./applyDefaults2.scala:11:12
[error] None of the overloaded alternatives of method apply in object Person with types
[error] (): Person
[error] (firstName: String, lastName: String, birthYear: Int, address: String): Person
[error] match arguments (("John" : String), ("Doe" : String))
[error] val p1 = Person("John", "Doe")
[error] ^^
Expectation: should work.
Possible explanations as discussed with @odersky and posted to StackOverflow:
The problem appears to stem from the interaction between universal apply methods and default parameter values. Essentially, the compiler automatically generates apply methods that forward to constructors in the companion object of classes. However, these methods do not include default parameters. Hypothesis: when there is a single constructor, the compiler is able to trace back to the original constructor's default parameters, but not when there are multiple constructors.
The text was updated successfully, but these errors were encountered:
This issue was picked for the Issue Spree No. 41 of January 16th, 2024 which takes place in 2 days. @dwijnand, @KuceraMartin, @aherlihy will be working on it. If you have any insight into the issue or guidance on how to fix it, please leave it here.
Reported by DallogFheir on StackOverflow.
Let
person.scala
be:Running with
scala-cli run -S 3.nightly applyDefaults2.scala
, we get:Expectation: should work.
Possible explanations as discussed with @odersky and posted to StackOverflow:
The text was updated successfully, but these errors were encountered: