Skip to content

Add case-class-companion incompatibility #138

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

Merged
merged 1 commit into from
Feb 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/incompatibilities/other-changed-features.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,6 @@ title: Other Changed Features

```scala mdoc:file:incompat-30/wildcard-argument/README.md
```

```scala mdoc:file:incompat-30/case-class-companion/README.md
```
2 changes: 1 addition & 1 deletion docs/incompatibilities/table.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ Some proven features are simplified or restricted to make the language easier an
|[Non-private constructor in private class](other-changed-features.md#non-private-constructor-in-private-class)||warning|||
|[Reflective call](other-changed-features.md#reflective-call)||||Type inference problem|
|[Wildcard type argument](other-changed-features.md#wildcard-type-argument)|||||

|[Case class companion](other-changed-features.md#case-class-companion)|||||

## Implicit Resolution

Expand Down
29 changes: 29 additions & 0 deletions incompat-30/case-class-companion/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
## Case class companion

The companion object of a case class does not extend any of the `Function{0-23}` traits anymore.
In particular, it does not inherit their methods: `tupled`, `curried`, `andThen`, `compose`...

For instance, the following lines of code are invalid.

```scala
case class Foo(x: Int, b: Boolean)

Foo.curried(1)(true)
Foo.tupled((2, false))
```

A cross-compiling solution is to explicitly eta-expand the method `Foo.apply`.

```scala
(Foo.apply _).curried(1)(true)
(Foo.apply _).tupled((2, false))
```

Or, for performance reason, you can introduce an intermediate function value.

```scala
val fooCtr: (Int, Boolean) => Foo = (x, b) => Foo(x, b)

fooCtr.curried(1)(true)
fooCtr.tupled((2, false))
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package foo

case class Foo(x: Int, b: Boolean)

object Bar {
val f1 = Foo.curried(1)(true)
val f2 = Foo.tupled((2, false))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package foo

case class Foo(x: Int, b: Boolean)

object Bar {
val f1 = (Foo.apply _).curried(1)(true)
val f2 = (Foo.apply _).tupled((1, true))

val fooCtr: (Int, Boolean) => Foo = (x, b) => Foo(x, b)

val f1Bis = fooCtr.curried(1)(true)
val f2Bis = fooCtr.tupled((2, false))
}
4 changes: 3 additions & 1 deletion incompat.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,8 @@ lazy val otherIncompats = Seq[ProjectReference](
ambiguousConversion,
implicitView,
typeOfImplicitDef,
viewBound
viewBound,
caseClassCompanion
)

// Syntactic incompatibilities
Expand All @@ -172,6 +173,7 @@ lazy val existentialType = project.in(file("incompat-30/existential-type")).sett
lazy val wildcardArgument = project.in(file("incompat-30/wildcard-argument")).settings(incompat30Settings)
lazy val explicitCallToUnapply = project.in(file("incompat-30/explicit-call-to-unapply")).settings(incompat30Settings)
lazy val reflectiveCall = project.in(file("incompat-30/reflective-call")).settings(incompat30Settings)
lazy val caseClassCompanion = project.in(file("incompat-30/case-class-companion")).settings(incompat30Settings)

// Contextual abstraction incompatibilities
lazy val ambiguousConversion =
Expand Down