-
Notifications
You must be signed in to change notification settings - Fork 3.1k
add Either.left and Either.right typed as Either #6329
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
Conversation
src/library/scala/util/Either.scala
Outdated
| */ | ||
| def left[B]: FromLeftPartiallyApplied[B] = new FromLeftPartiallyApplied[B] | ||
|
|
||
| private[util] final class FromLeftPartiallyApplied[B](private val dummy: Boolean = true) extends AnyVal { |
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 think these dummys can now be Unit since 2.12.3 :)
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.
| */ | ||
| def left[B]: FromLeftPartiallyApplied[B] = new FromLeftPartiallyApplied[B] | ||
|
|
||
| private[util] final class FromLeftPartiallyApplied[B](private val dummy: Unit) extends AnyVal { |
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.
@why private[util] here (and 14 lines down), why not fully private? (Guess: is it because otherwise you get a warning about the type escaping...?)
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.
are there any pros/cons to using Unit vs Boolean in this pattern?
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'm pretty sure I've tried to make it fully private, but compiler wasn't happy. I will double check it
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.
As for Unit, i guess it's just less to type, no need to give it default value. In any case it's just a trick for AnyVal.
Maybe someone has a better idea here, as I don't see much difference otherwise
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 think I saw somewhere that using Unit didn't work until Scala 2.12.3. that would explain it
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.
as expected
[error] /home/jozic/projects/github/scala/src/library/scala/util/Either.scala:468: private class FromLeftPartiallyApplied escapes its defining scope as part of type scala.util.Either.FromLeftPartiallyApplied[B]
[error] def left[B]: FromLeftPartiallyApplied[B] = new FromLeftPartiallyApplied[B]
[error] ^
[error] one error found
[error] (library/compile:compileIncremental) Compilation failed
| if (test) Right(right) else Left(left) | ||
|
|
||
| /** | ||
| * Creates `FromLeftPartiallyApplied[B]` that given an argument of type `A` |
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.
Since this name appears in the Scaladoc, perhaps a shorter name could be found?
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.
Do you have any good ideas? I think the name is long, but very descriptive
| * //l: Either[String, Int] = Left("boo") | ||
| * }} | ||
| */ | ||
| def left[B]: FromLeftPartiallyApplied[B] = new FromLeftPartiallyApplied[B] |
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.
It's my first time seeing this pattern, so it isn't obvious to me (and might not be obvious to other reviewers) what the downsides of this pattern are. I mean, obviously it's a downside that this extra type is involved (and shows up in the Scaladoc). Are there any concealed footguns here?
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.
that was my first time as well, but it looks safe enough, except the fact you mentioned - it appears in public
like you can do Either.left[Int] and get instance of FromLeftPartiallyApplied[Int]
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'm fairly sure there's no footguns here :)
|
why doesn't Cats use this pattern, why does it have the #6328 version instead? core/src/main/scala/cats/syntax/either.scala: if this is merged, is it going to be make difficulties for Cats? (offhand, I guess you'd need separate |
|
That's a good question, though I think most cats users use the more convenient |
|
I guess @LukaJCB is right, cats is full of implicits and @LukaJCB you meant deprecate |
yeah I'm not really comfortable with putting extension methods on |
|
Needs a rebase, given #6681 introduced EitherTest too. |
|
rebased on 2.13.x |
|
Another approach would be adding methods to |
|
@lrytz I guess that also would work |
|
I'd say we can just discuss here. But if we go that other way, a new PR is less confusing for future us. |
|
👋 what's the status here? |
|
@adriaanm it went through all checks, and to my understanding lacks only acceptance of someone from the team (that could be you :)) @SethTisue do you have an idea what should the next step be? |
|
@jozic Lukas's suggestion is a lot simpler and on that grounds alone seems preferable to me. so yes, new PR with that, please. I'll provisionally close this one. |
|
@SethTisue new PR is #7011 |
this is another implementation of #6328 inspired by @LukaJCB and others in gitter(https://gitter.im/typelevel/general?at=5a85d126ce68c3bc748f61d4) (thanks!)
motivation:
LeftandRightconstructors are typed asLeftandRightrespectively, but sometimes we want something likeOption.emptywhich returnsNonebut typed asOption.The test represents a code that would not compile if plain
Left/Rightconstructors are used@SethTisue please check it again, thanks!
If this looks better (it does to me) then #6328 should be closed