Skip to content
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 Applicative.unit #1586

Merged
merged 2 commits into from
Apr 4, 2017
Merged

Add Applicative.unit #1586

merged 2 commits into from
Apr 4, 2017

Conversation

alexandru
Copy link
Member

@alexandru alexandru commented Apr 4, 2017

Adding a short-hand to Applicative for F.pure(()).

@typeclass trait Applicative[F[_]] extends Apply[F] { self =>
  /**
   * Returns an `F[Unit]` value, equivalent with `pure(())`.
   *
   * A useful shorthand, also allowing implementations to optimize the
   * returned reference (e.g. it can be a `val`).
   */
  def unit: F[Unit] = pure(())

Reasons:

  1. I find myself doing F.pure(()).map(_ => a) a lot (due to pure not taking a by-name param and having no way to do that otherwise, but that's another discussion)
  2. F[Unit] is often used to indicate callbacks that are going to trigger side-effects, like for resource clean-ups - for example in Task.doOnFinish and we often need to create empty F[Unit] instances that don't do anything; F.pure(()) is more noisy versus F.unit
  3. Applicative implementations can optimize this to always return the same reference, instead of rebuilding pure(()) over and over again

Prior art:

scala.concurrent.Future.unit

@mpilquist
Copy link
Member

👍

@codecov-io
Copy link

codecov-io commented Apr 4, 2017

Codecov Report

Merging #1586 into master will increase coverage by <.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1586      +/-   ##
==========================================
+ Coverage   92.42%   92.42%   +<.01%     
==========================================
  Files         249      249              
  Lines        3971     3974       +3     
  Branches      146      131      -15     
==========================================
+ Hits         3670     3673       +3     
  Misses        301      301
Impacted Files Coverage Δ
.../scala/cats/laws/discipline/ApplicativeTests.scala 100% <100%> (ø) ⬆️
core/src/main/scala/cats/Applicative.scala 75% <100%> (+2.27%) ⬆️
...aws/src/main/scala/cats/laws/ApplicativeLaws.scala 100% <100%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update e77bb99...dba946c. Read the comment docs.

@kailuowang kailuowang merged commit 43d205f into typelevel:master Apr 4, 2017
@kailuowang kailuowang modified the milestone: 1.0.0-MF Apr 26, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants