Comments (6)
Honestly, I don't know why whenA
and unlessA
have call-by-value semantic for F[A]
in mouse
for sure, I can only make a hypothesis. Underlying methods in the Applicative
have call-by-name semantic (even in Scalaz https://github.com/scalaz/scalaz/blob/ea81ca782a634d4cd93c56529c082567a207c9f6/core/src/main/scala/scalaz/syntax/std/BooleanOps.scala#L218).
However, these methods on Applicative
syntax have call-by-value semantic for F[A]
see typelevel/cats#3899.
So that could be the reason for the same behavior in the mouse
.
Applicative[List].whenA(false)(List(println("Direct method"))) // will print nothing
List(println("Syntax method")).whenA(false) // will print
false.whenA(List(println("Syntax method"))) // will print
And I am convinced we can't change that semantic due to bincompat guarantees.
Probably you can use fold
to have call-by-name semantic.
from mouse.
I would find it confusing that there's two method pairs that have completely different names but do almost the same thing. Not making them by-name was a mistake originally that we need to recover from as gracefully as possible. Unfortunately it claimed the best method name, but I'd still like a variant on that.
I didnt get any feedback to my comment on cats issue, so let's make a local decision.
Like whenA
and unlessA
but lazy in the args. Options:
whenAL
and unlessAL
: L means lazy. clear what L suffix means once you know it, but not before.
whenA_
and unlessA_
: _ means 'variant' I guess. Visually nicer. Overloads _ to mean different things as notes in the cats issue, not a clear principle. But maybe thats OK, its a generic suffix that indicates a variant method.
L is my pref suffix, but I'd happily meet you on a _ suffix (over a different name) so we can move forwards..?
from mouse.
I think this is an unfortunate oversight (aka "bug"). My bad for not picking up at PR time.
So, how do we move forward, accepting the constraint that we can't break existing callers of whenA
etc?
Im open to
- Add lazy variants of
whenA
/unlessA
. What to call them? Do we have any precedents in Typelevel ecosystem to follow? - Put some policy in DEV.md clarifying that operators that take conditionally executed code blocks should generally by by-name.
from mouse.
We could add lazy variants for these methods, I agree. Probably whenA_
and unlessA_
for names is good? Anyway, I have no other suggestions.
from mouse.
In the cats issue, Oscar mentions that the underscore suffix has a different implication.
Another alternative is L
for Lazy; whenAL
& unlessAL
. Its short and the reason for the L is at least sane, although not immediately self-evident
from mouse.
Also, we could have different names for those methods - ifTrueF
and ifFalseF
:
val boolean: Boolean = ???
def computation[F[_]: Applicative]: IO[Unit] = Applicative[F].unit
val result1: IO[Unit] = boolean.ifTrueF(computation[IO]) // same as `whenA` but lazy one
val result2: IO[Unit] = boolean.ifFalseF(computation[IO]) // same as `unlessA` but lazy one
What do you think @benhutchison @diversit?
from mouse.
Related Issues (20)
- Publish for Cats 2.1.0? HOT 1
- Offer of code, int/long to le/be endian array and string to zero terminated array support HOT 1
- Scala 3 migration HOT 4
- Add whenA/unlessA extension methods for Boolean HOT 1
- mapKeys method isn't pure HOT 2
- Proposal: rename master to main HOT 3
- Microsite for `mouse` HOT 2
- Which Scala 3 version should be the base for `mouse`? HOT 4
- please review notes for 1.0.8 release HOT 2
- consider merging into cats HOT 7
- bad option `-doc-external-doc` on Scala 3 HOT 6
- Fix publishing HOT 2
- Adding mapValuesF to MapOps HOT 3
- Adding `toRightIn` and `toOptionIn` HOT 4
- Adding `mapInF` HOT 3
- Proposal: Enhance navigation in the root `mouse` package HOT 4
- Suggestion: Add `parFlatMapN` HOT 4
- Proposal: Renaming this library HOT 6
- parseUrl for String on the JVM HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mouse.