Skip to content

Topic/tap each specialized #79

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
wants to merge 2 commits into from

Conversation

gdiet
Copy link
Contributor

@gdiet gdiet commented Apr 10, 2021

Add specialised version of tapEach to scala.Option to preserve the type.

The actual addition is in the second commit fb1f19a.

See also https://stackoverflow.com/questions/67017901/why-does-scala-option-tapeach-return-iterable-not-option?noredirect=1#comment118462412_67017901

* collection" contract even though it seems unlikely to matter much in a
* collection with max size 1.
*/
class WithFilter(p: A => Boolean) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason for this not to be final?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I took the code of scala.Option from the 2.13.x branch. All your findings relate to that code. As @NthPortal points out below writing replacements for existing classes is not the way to go for this repository, so I'll provide an extension method instead (probably in a new PR, that's cleaner).

*/
@SerialVersionUID(1234815782226070388L) // value computed by serialver for 2.11.2, annotation added in 2.11.4
final case class Some[+A](value: A) extends Option[A] {
def get: A = value

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def get: A = value
override def get: A = value

*/
@SerialVersionUID(5066590221178148012L) // value computed by serialver for 2.11.2, annotation added in 2.11.4
case object None extends Option[Nothing] {
def get: Nothing = throw new NoSuchElementException("None.get")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def get: Nothing = throw new NoSuchElementException("None.get")
override def get: Nothing = throw new NoSuchElementException("None.get")

* }
* }}}
*/
final def isDefined: Boolean = !isEmpty

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
final def isDefined: Boolean = !isEmpty
final def isDefined: Boolean = this ne None

Copy link
Contributor

@NthPortal NthPortal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this library cannot provide replacements for existing library classes. it must instead provide extension methods. and it's possible that it won't have higher priority than the existing conversion

@gdiet
Copy link
Contributor Author

gdiet commented Apr 12, 2021

@NthPortal Sorry, I overlooked in the README that changes/additions to existing library classes should be coded as extension methods.

I will open a new PR with the specialized extension method, replacing this PR - I think it's cleaner that way...

If I provide a specialized tapEach extension for Option, will this stay an extension in Scala 3.1 (which I wouldn't like too much) or will the specialized extension method be "merged" into scala.Option directly eventually? If the latter, would it be a good idea to add a comment how this could be done?

@NthPortal
Copy link
Contributor

to be honest, I'm suspicious the conversion to Seq in Option will have a higher priority than any extension method we add in this library, making this change impossible until 3.T, but you'd have to test to find out

@gdiet
Copy link
Contributor Author

gdiet commented Apr 13, 2021

Superseded by PR #80

@gdiet gdiet closed this Apr 13, 2021
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 this pull request may close these issues.

3 participants