Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10648 from som-snytt/issue/3664-case-function
Accommodate case companion as function
- Loading branch information
Showing
13 changed files
with
239 additions
and
38 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
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
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,6 @@ | ||
t3664.scala:9: warning: Synthetic case companion used as a Function, use explicit object with Function parent | ||
def f(xs: List[Int]): List[C] = xs.map(C) | ||
^ | ||
error: No warnings can be incurred under -Werror. | ||
1 warning | ||
1 error |
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,10 @@ | ||
//> using options -Werror -Xlint -Xsource:3 | ||
|
||
// use -Xsource:3 to warn that implicitly extending Function is deprecated | ||
// use -Xsource:3-cross for dotty behavior: no extend Function, yes adapt C.apply.tupled | ||
|
||
case class C(i: Int) | ||
|
||
class Test { | ||
def f(xs: List[Int]): List[C] = xs.map(C) | ||
} |
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,6 @@ | ||
t3664b.scala:9: warning: The method `apply` is inserted. The auto insertion will be deprecated, please write `C.apply` explicitly. | ||
def f(xs: List[Int]): List[C] = xs.map(C) | ||
^ | ||
error: No warnings can be incurred under -Werror. | ||
1 warning | ||
1 error |
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,10 @@ | ||
//> using options -Werror -Xlint -Xsource:3-cross | ||
|
||
// use -Xsource:3 to warn that implicitly extending Function is deprecated | ||
// use -Xsource:3-cross for dotty behavior: no extend Function, yes adapt C.apply.tupled | ||
|
||
case class C(i: Int) | ||
|
||
class Test { | ||
def f(xs: List[Int]): List[C] = xs.map(C) | ||
} |
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,27 @@ | ||
t3664c.scala:20: error: type mismatch; | ||
found : C.type | ||
required: ((Int, Int, Int)) => C | ||
def f(xs: List[(Int, Int, Int)]): List[C] = xs.map(C) // hard error | ||
^ | ||
t3664c.scala:22: error: type mismatch; | ||
found : ((Int, Int)) => C | ||
required: ((Int, Int, Int)) => C | ||
def g(xs: List[(Int, Int, Int)]): List[C] = xs.map(C.tupled) // hard error | ||
^ | ||
t3664c.scala:24: error: type mismatch; | ||
found : D.type | ||
required: ((Int, Int)) => D | ||
def d(xs: List[(Int, Int)]): List[D] = xs.map(D) // hard error | ||
^ | ||
t3664c.scala:26: warning: An unapplied 0-arity method was eta-expanded (due to the expected type () => E), rather than applied to `()`. | ||
Write E.apply() to invoke method apply, or change the expected type. | ||
val e: () => E = E | ||
^ | ||
t3664c.scala:26: warning: The method `apply` is inserted. The auto insertion will be deprecated, please write `E.apply` explicitly. | ||
val e: () => E = E | ||
^ | ||
t3664c.scala:28: warning: The method `apply` is inserted. The auto insertion will be deprecated, please write `F.apply` explicitly. | ||
def ov(xs: List[Int]): List[F] = xs.map(F) | ||
^ | ||
3 warnings | ||
3 errors |
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,29 @@ | ||
//> using options -Werror -Xlint -Xsource:3-cross | ||
|
||
// use -Xsource:3 to warn that implicitly extending Function is deprecated | ||
// use -Xsource:3-cross for dotty behavior: no extend Function, yes adapt C.apply.tupled | ||
|
||
case class C(i: Int, j: Int) | ||
|
||
abstract case class D(i: Int, j: Int) | ||
|
||
case class E() | ||
|
||
case class F(i: Int) | ||
object F { | ||
def apply(): F = apply(42) | ||
def apply(i: Int): F = new F(i) | ||
def apply(i: Int, j: Int): F = new F(i+j) | ||
} | ||
|
||
class Test { | ||
def f(xs: List[(Int, Int, Int)]): List[C] = xs.map(C) // hard error | ||
|
||
def g(xs: List[(Int, Int, Int)]): List[C] = xs.map(C.tupled) // hard error | ||
|
||
def d(xs: List[(Int, Int)]): List[D] = xs.map(D) // hard error | ||
|
||
val e: () => E = E | ||
|
||
def ov(xs: List[Int]): List[F] = xs.map(F) | ||
} |
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 @@ | ||
|
||
//> using options -Werror -Xlint -Xsource:3-cross | ||
|
||
import language.implicitConversions | ||
|
||
case class C(i: Int) | ||
object C // no function parent | ||
|
||
// use conversion, don't warn about apply insertion | ||
class Test { | ||
implicit def cv(c: C.type): Function[Int, C] = C(_) | ||
def f(xs: List[Int]): List[C] = xs.map(C) | ||
} |
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,12 @@ | ||
t3664.scala:10: warning: The method `apply` is inserted. The auto insertion will be deprecated, please write `B.apply` explicitly. | ||
def mapped(xs: List[Int]): List[B] = xs.map(B) | ||
^ | ||
t3664.scala:13: warning: The method `apply` is inserted. The auto insertion will be deprecated, please write `(C.apply _).tupled` explicitly. | ||
def cross(xs: List[(Int, Int)]): List[C] = xs.map(C) | ||
^ | ||
t3664.scala:15: warning: The method `apply` is inserted. The auto insertion will be deprecated, please write `(C.apply _).tupled` explicitly. | ||
def f(xs: List[(Int, Int)]): List[C] = xs.map(C.tupled) | ||
^ | ||
t3664.scala:17: warning: The method `apply` is inserted. The auto insertion will be deprecated, please write `(C.apply _).curried` explicitly. | ||
def g(xs: List[Int]): List[C] = xs.map(C.curried).map(_(42)) | ||
^ |
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,30 @@ | ||
//> using options -Xlint -Xsource:3-cross | ||
|
||
// use -Xsource:3 to warn that implicitly extending Function is deprecated | ||
// use -Xsource:3-cross for dotty behavior: no extend Function, yes adapt C.apply.tupled | ||
|
||
case class B(i: Int) | ||
case class C(i: Int, j: Int) | ||
|
||
class Test { | ||
def mapped(xs: List[Int]): List[B] = xs.map(B) | ||
|
||
// accept for cross because dotty has no C.tupled but has fancy untupling adaptation | ||
def cross(xs: List[(Int, Int)]): List[C] = xs.map(C) | ||
|
||
def f(xs: List[(Int, Int)]): List[C] = xs.map(C.tupled) | ||
|
||
def g(xs: List[Int]): List[C] = xs.map(C.curried).map(_(42)) | ||
|
||
def f2(xs: List[(Int, Int)]): List[C] = xs.map((C.apply _).tupled) | ||
|
||
def g2(xs: List[Int]): List[C] = xs.map((C.apply _).curried).map(_(42)) | ||
|
||
def g3(xs: List[Int]): List[C] = xs.map(((i: Int, j: Int) => C.apply(i, j)).curried).map(_(42)) | ||
} | ||
object Test extends Test with App { | ||
assert(mapped(List(52)) == List(B(52))) | ||
assert(cross(List(27->42)) == List(C(27, 42))) | ||
assert(f(List(27->42)) == List(C(27, 42))) | ||
assert(g(List(27)) == List(C(27, 42))) | ||
} |