Add PartialFunction instance for Profunctor typeclass#3392
Conversation
| */ | ||
| override def dimap[A, B, C, D](fab: PartialFunction[A, B])(f: C => A)(g: B => D): PartialFunction[C, D] = | ||
| new PartialFunction[C, D] { | ||
| override def isDefinedAt(c: C): Boolean = fab.isDefinedAt(f(c)) |
There was a problem hiding this comment.
I don't think if there's any way out of running f twice
Codecov Report
@@ Coverage Diff @@
## master #3392 +/- ##
==========================================
- Coverage 92.70% 92.48% -0.23%
==========================================
Files 379 379
Lines 7981 7960 -21
Branches 230 226 -4
==========================================
- Hits 7399 7362 -37
- Misses 582 598 +16
Continue to review full report at Codecov.
|
|
I wonder if we could add instances for the other parts of the |
|
@LukaJCB I updated the same PR instead, is it okay? |
LukaJCB
left a comment
There was a problem hiding this comment.
Hey @gagandeepkalra sorry for keeping you waiting for so long, this looks amazing, thanks! Do you have time to rebase with master? :)
ff84eea to
6af528d
Compare
Codecov Report
@@ Coverage Diff @@
## master #3392 +/- ##
==========================================
+ Coverage 91.63% 92.05% +0.42%
==========================================
Files 382 383 +1
Lines 8304 8347 +43
Branches 232 210 -22
==========================================
+ Hits 7609 7684 +75
+ Misses 695 663 -32 |
|
Thank you @LukaJCB @barambani for the review, learnt something new today. |
|
Thank you.
same here. |
LukaJCB
left a comment
There was a problem hiding this comment.
Hey, thanks! This looks great, but I wonder why you only placed it in the companion objects of Profunctor and Compose, maybe I'm missing something?
|
Ah of course! Sorry I misremembered a detail of the whole thing |
travisbrown
left a comment
There was a problem hiding this comment.
👍 to the concept—I'm not sure why we hadn't done this before. I do have a couple of comments / questions about details.
|
|
||
| object Compose { | ||
| implicit def catsInstancesForFunction1: ArrowChoice[Function1] with CommutativeArrow[Function1] = | ||
| implicit val catsInstancesForFunction1: ArrowChoice[Function1] with CommutativeArrow[Function1] = |
There was a problem hiding this comment.
It's not really a big deal, but there is a reason for these to be methods instead of vals: they're already instantiated as vals in the instances objects, so using def here won't result in new instantiations, but using a val does result in a little extra overhead and indirection, since it generates both an accessor method and a field.
In general this is unlikely to matter much, but because there are a lot of these, we've had a policy of def instead of val.
There was a problem hiding this comment.
I see, that's a nice way of saving up on a reference; apologies for oversight
| with AllInstancesBinCompat4 | ||
| with AllInstancesBinCompat5 | ||
| with AllInstancesBinCompat6 | ||
| with AllInstancesBinCompat7 |
There was a problem hiding this comment.
We shouldn't need any new BinCompat traits now that we've dropped 2.11. You should be able to add PartialFunctionInstances directly to AllInstances, I believe?
There was a problem hiding this comment.
I'm not sure, getting this error when adding to AllInstances; I don't see anything obvious, might need some help
[error] Cats core: Failed binary compatibility check against org.typelevel:cats-core_2.12:1.0.1! Found 2 potential problems (filtered 481)
[error] * abstract synthetic method cats$instances$PartialFunctionInstances$setter$catsStdInstancesForPartialFunction_=(cats.arrow.ArrowChoice)Unit in interface cats.instances.PartialFunctionInstances is inherited by class AllInstances in current version.
[error] filter with: ProblemFilters.excludeInheritedNewAbstractMethodProblem
[error] * abstract method catsStdInstancesForPartialFunction()cats.arrow.ArrowChoice in interface cats.instances.PartialFunctionInstances is inherited by class AllInstances in current version.
[error] filter with: ProblemFilters.excludeInheritedNewAbstractMethodProblem
There was a problem hiding this comment.
@gagandeepkalra Hmm, thanks, I'll take a look now.
|
Oh, right. The issue is that adding a |
|
Here's some explanation of the issue: #2789 (comment) I think that something like this should work: trait PartialFunctionInstances {
def catsStdInstancesForPartialFunction: ArrowChoice[PartialFunction] with CommutativeArrow[PartialFunction] =
PartialFunctionInstances.instance
}
private object PartialFunctionInstances {
private val instance: ArrowChoice[PartialFunction] with CommutativeArrow[PartialFunction] =
new ArrowChoice[PartialFunction] with CommutativeArrow[PartialFunction] {
// ...I'd be happy to merge this PR as-is, and I can fix this particular part in a follow-up, if you'd prefer? |
travisbrown
left a comment
There was a problem hiding this comment.
There's one remaining issue, but I think it could be handled in a follow-up PR, so I'm approving this now. Thanks again @gagandeepkalra!
…improved doctests with less imports now needed
|
@travisbrown thank you, summarising- I understand why adding a def to trait won't break bc given we have method defaults in java 8 interface, while though Meanwhile I had another doc-test import related improvement, so have pushed both together. |
travisbrown
left a comment
There was a problem hiding this comment.
@gagandeepkalra Thanks, that looks good to me! I don't it necessarily needs a second re-review, but I'll leave it for right now for @LukaJCB to take a look if he wants.
I don't remember the details about why adding a val to a trait breaks bincompat in 2.12, but right, it has something to default methods (and the implementation of initialization using static forwarder methods on the companion object).
|
Going ahead and merging this since it had an earlier second 👍. |
resolves #3386