-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Add scala.util.control.TailCalls.TailRec instances #3041
Conversation
@@ -378,6 +378,7 @@ sealed abstract private[cats] class EvalInstances extends EvalInstances0 { | |||
def flatMap[A, B](fa: Eval[A])(f: A => Eval[B]): Eval[B] = fa.flatMap(f) | |||
def extract[A](la: Eval[A]): A = la.value | |||
def coflatMap[A, B](fa: Eval[A])(f: Eval[A] => B): Eval[B] = Later(f(fa)) | |||
override def unit: Eval[Unit] = Eval.Unit |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unrelated, but I noticed we could add this and avoid allocations for some folks that use Applicative[F].unit
with Eval
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add an object tailRec extends TailRecInstances
to the instances
package object? Unfortunately we've had to make these version-specific, so you'll have to do it twice.
@@ -378,6 +378,7 @@ sealed abstract private[cats] class EvalInstances extends EvalInstances0 { | |||
def flatMap[A, B](fa: Eval[A])(f: A => Eval[B]): Eval[B] = fa.flatMap(f) | |||
def extract[A](la: Eval[A]): A = la.value | |||
def coflatMap[A, B](fa: Eval[A])(f: Eval[A] => B): Eval[B] = Later(f(fa)) | |||
override def unit: Eval[Unit] = Eval.Unit |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is unrelated?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes. I added a comment, I just noticed it was missing while looking at Eval.
@@ -30,14 +31,12 @@ class TrampolineBench { | |||
y <- Trampoline.defer(trampolineFib(n - 2)) | |||
} yield x + y | |||
|
|||
// TailRec[A] only has .flatMap in 2.11. | |||
@Benchmark |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch. Doing a search for "2.10" shows four or five other things like this we should do, but none of them look urgent.
@@ -61,4 +61,7 @@ trait AllInstancesBinCompat4 extends SortedMapInstancesBinCompat1 with MapInstan | |||
|
|||
trait AllInstancesBinCompat5 extends SortedSetInstancesBinCompat0 | |||
|
|||
trait AllInstancesBinCompat6 extends SortedSetInstancesBinCompat1 with SortedMapInstancesBinCompat2 | |||
trait AllInstancesBinCompat6 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doesn't this break bincompat on 2.11? Only with the recent release candidates, I guess, so maybe it's fine?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would assume so. I think we should only bump mima on actual releases (not including any pre-release).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@johnynek Right, but it's still useful to know, so we can decide whether we need to publish new pre-releases of cats-effect or Circe after a Cats pre-release, etc.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the plan is to drop 2.11 on master immediately after 2.0.0 release (this evening). So we can add this to the root trait.
Codecov Report
@@ Coverage Diff @@
## master #3041 +/- ##
=========================================
+ Coverage 93.57% 93.6% +0.03%
=========================================
Files 371 376 +5
Lines 7097 7839 +742
Branches 185 228 +43
=========================================
+ Hits 6641 7338 +697
- Misses 456 501 +45
Continue to review full report at Codecov.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, looks good to me!
@kailuowang any concerns with merging? |
@johnynek sorry it was too last minute to be carried on the 2.0.0 train. |
@kailuowang I'm confused why we can't merge now, and then when you make the change to merge in the trait and drop 2.11 everything is fine? |
@johnynek we can't merge the existing bincompat traits because of BC. If we merge this now then we need to It's just easier if we do it the other order. I am already working on dropping 2.11 on master so this shouldn't be held for long. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Just formatted, which should fix the build. |
Now there's a bincompat error, and it looks legit. I don't know how this ever passed, given that adding a |
I guess the idea that we can just add new traits to AllInstances in 2.0.0 is wrong in some way. |
I would welcome your push, as my vanity has a slight preference for this PR be the one merged. |
Thank you for doing this work. |
@johnynek Pushed, thanks! (They'd be your commits either way. 😄) |
👍 Thanks @kailuowang and @travisbrown |
Adds Defer and Monad instances for TailRec. Now that we don't support 2.10 anymore, we can also re-enable the benchmarks. For my (somewhat slow) machine, TailRec is about 17% faster than Trampoline (but much slower than Eval).
The motivation to add this are for users interacting with code that already uses TailRec and for instance interpreting Free into TailRec, or someone who wants to use TailRec with generic code that uses Defer (e.g. ContT).
We could add Semigroup, Monoid and Group to kernel. Since this is further from the main use of TailRec and independent of this current PR, I will not include it here.