Skip to content

Should we continue to generate function parents for case companions? #6190

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

Closed
odersky opened this issue Mar 28, 2019 · 5 comments · Fixed by #7207
Closed

Should we continue to generate function parents for case companions? #6190

odersky opened this issue Mar 28, 2019 · 5 comments · Fixed by #7207

Comments

@odersky
Copy link
Contributor

odersky commented Mar 28, 2019

We currently generate a function type parent type for case class companions like this. Given:

case class C(a: A)

the companion is

object C extends (A => C) { ... }

However, there are many restrictions. We do not generate a function type for the companion, if

  • the companion is already written
  • the case class is polymorphic
  • the case class has more than one parameter list
  • the constructor of the class is private or qualified private
  • the class is an enum case, see: 7ccc244 of Change enum Desugarings #6154.

The question is, given all these restrictions, should we stop generating function parents altogether? It would mean one has to write C(_) instead of C to convert C to a function, but that's probably not such a big deal.

@nafg
Copy link

nafg commented Mar 28, 2019 via email

@ritschwumm
Copy link

i'd remove it:

it's frustrating when you try to use C as a function and the compiler tells you "no, that works elsewhere but not in this case, please try again differently". it's frustrating when you add a type parameter to your case class and suddenly you have to change every place that was relying on the companion being a function.

personally, i made it a habit to always write C.apply to avoid these (small) frustrations so i won't miss it at all.

@nafg
Copy link

nafg commented Mar 28, 2019 via email

@odersky
Copy link
Contributor Author

odersky commented Mar 28, 2019

Maybe one can compensate for the loss by upgrading automatic conversions. Specifically, try to insert apply if an expression is not compatible with its expected function type.

@hrhino
Copy link
Contributor

hrhino commented Apr 2, 2019

I've never liked this, so 👍 to its removal.

I don't know how the auto-apply could help slick: in C.tupled the expected type of C (as I understand it) is ? { def tupled: ? } (maybe not in dotty?), so you'd need to be willing to treat that auto-apply similarly to an implicit conversion: there's no function type expected.

That said, Slick is how I first ran into this irregularly way back when. Is there a reason why (C.apply.tupled, C.unapply) isn't any good (other than its relative verbosity)? I don't think it works in Scala 2, but IIRC dotty is a bit more willing to eta-expand... would that work?

liufengyun added a commit to dotty-staging/dotty that referenced this issue Sep 12, 2019
liufengyun added a commit to dotty-staging/dotty that referenced this issue Sep 15, 2019
liufengyun added a commit to dotty-staging/dotty that referenced this issue Dec 7, 2020
liufengyun added a commit to dotty-staging/dotty that referenced this issue Dec 7, 2020
odersky added a commit that referenced this issue Dec 9, 2020
Fix #6190: eta-expand companion object if functions are expected
jarmuszz added a commit to jarmuszz/fs2-data that referenced this issue Jun 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants