Skip to content
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

New incompatibility: case class companions don’t extend Function anymore #122

Closed
julienrf opened this issue Dec 31, 2020 · 2 comments · Fixed by #138
Closed

New incompatibility: case class companions don’t extend Function anymore #122

julienrf opened this issue Dec 31, 2020 · 2 comments · Fixed by #138

Comments

@julienrf
Copy link
Contributor

In Scala 2, a case class companion was extending Function.

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

// The Scala 2 compiler synthesizes the following companion
object Foo extends Function2[Int, Boolean, Foo] {
  def apply(x: Int, b: Boolean) = new Foo(x, b)
}

This is not the case anymore, which means that companions don’t inherit function methods such as apply, tupled, andThen, and compose.

One work-around to write code that cross-compiles is to manually write a function that calls the constructor:

val fooConstructor: (Int, Boolean) => Foo = (x, b) => Foo(x, b)
// Then, `fooConstructor` has all the methods of Function
@adpi2
Copy link
Member

adpi2 commented Feb 15, 2021

As a work-around, we can also use the apply method because it can be eta-expanded to a Function2

Foo.apply.curried(1)(true) // compiles in Scala 2.13 and Scala 3.0.0-M3

What do you think @julienrf ?

@adpi2
Copy link
Member

adpi2 commented Feb 15, 2021

Actually it doesn't compile in Scala 2.13:

[error] Unapplied methods are only converted to functions when a function type is expected.
[error] You can make this conversion explicit by writing `apply _` or `apply(_,_)` instead of `apply`.
[error]   val foo1 = Foo.apply.curried(1)(true)

But this one cross-compiles:

(Foo.apply _).curried(1)(true)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants