-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow user-defined
[un]apply
in case companion
Don't emit a synthetic `apply` (or `unapply`) when it would clash with an existing one. This allows e.g., a `private apply`, along with a `case class` with a `private` constructor. We have to retract the synthetic method in a pretty roundabout way, as we need the other methods and the owner to be completed already. Unless we have to complete the synthetic `apply` while completing the user-defined one, this should not be a problem. If this does happen, this implies there's a cycle in computing the user-defined signature and the synthetic one, which is not allowed.
- Loading branch information
Showing
4 changed files
with
149 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
userdefined_apply.scala:3: error: overloaded method apply needs result type | ||
private def apply(x: Int) = if (x > 0) new ClashOverloadNoSig(x) else apply("") | ||
^ | ||
userdefined_apply.scala:12: error: overloaded method apply needs result type | ||
private def apply(x: Int) = if (x > 0) ClashRecNoSig(1) else ??? | ||
^ | ||
userdefined_apply.scala:19: error: overloaded method apply needs result type | ||
private def apply(x: Boolean) = if (x) NoClashNoSig(1) else ??? | ||
^ | ||
userdefined_apply.scala:26: error: overloaded method apply needs result type | ||
private def apply(x: Boolean) = if (x) NoClashOverload(1) else apply("") | ||
^ | ||
four errors found |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
object ClashOverloadNoSig { | ||
// error: overloaded method apply needs result type | ||
private def apply(x: Int) = if (x > 0) new ClashOverloadNoSig(x) else apply("") | ||
|
||
def apply(x: String): ClashOverloadNoSig = ??? | ||
} | ||
|
||
case class ClashOverloadNoSig private(x: Int) | ||
|
||
object ClashRecNoSig { | ||
// error: recursive method apply needs result type | ||
private def apply(x: Int) = if (x > 0) ClashRecNoSig(1) else ??? | ||
} | ||
|
||
case class ClashRecNoSig private(x: Int) | ||
|
||
object NoClashNoSig { | ||
// error: overloaded method apply needs result type | ||
private def apply(x: Boolean) = if (x) NoClashNoSig(1) else ??? | ||
} | ||
|
||
case class NoClashNoSig private(x: Int) | ||
|
||
object NoClashOverload { | ||
// error: overloaded method apply needs result type | ||
private def apply(x: Boolean) = if (x) NoClashOverload(1) else apply("") | ||
|
||
def apply(x: String): NoClashOverload = ??? | ||
} | ||
|
||
case class NoClashOverload private(x: Int) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// NOTE: the companion inherits a public apply method from Function1! | ||
case class NeedsCompanion private (x: Int) | ||
|
||
object ClashNoSig { // ok | ||
private def apply(x: Int) = if (x > 0) new ClashNoSig(x) else ??? | ||
} | ||
case class ClashNoSig private (x: Int) | ||
|
||
|
||
object Clash { | ||
private def apply(x: Int) = if (x > 0) new Clash(x) else ??? | ||
} | ||
case class Clash private (x: Int) | ||
|
||
object ClashSig { | ||
private def apply(x: Int): ClashSig = if (x > 0) new ClashSig(x) else ??? | ||
} | ||
case class ClashSig private (x: Int) | ||
|
||
object ClashOverload { | ||
private def apply(x: Int): ClashOverload = if (x > 0) new ClashOverload(x) else apply("") | ||
def apply(x: String): ClashOverload = ??? | ||
} | ||
case class ClashOverload private (x: Int) | ||
|
||
object NoClashSig { | ||
private def apply(x: Boolean): NoClashSig = if (x) NoClashSig(1) else ??? | ||
} | ||
case class NoClashSig private (x: Int) | ||
|
||
object NoClashOverload { | ||
// needs full sig | ||
private def apply(x: Boolean): NoClashOverload = if (x) NoClashOverload(1) else apply("") | ||
def apply(x: String): NoClashOverload = ??? | ||
} | ||
case class NoClashOverload private (x: Int) |