-
Notifications
You must be signed in to change notification settings - Fork 30
Conversation
…n to accumulate errors
Tries to clarify the docs in fromOption Nits in tests
* v1 is Validated.Valid && v2 is Validated.Invalid -> v2.toValidatedNel() | ||
* v1 is Validated.Invalid && v2 is Validated.Valid -> v1.toValidatedNel() | ||
* v1 is Validated.Invalid && v2 is Validated.Invalid -> Validated.Invalid(NonEmptyList(v1.e, listOf(v2.e))) | ||
* else -> throw IllegalStateException("Not possible value") |
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.
Instead of Throw
, we could return an Invalid with 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.
But that's the impossible case, does it really matter? Actually I was looking for if there was an operator that does this already and doesn't leave the burden of writing this to the dev but couldn't see any
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.
Isn't it exhaustive then?
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 cannot be because we're checking 2 elements
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.
we need pattern matching weep. I'd still use Invalid for the sake of not promoting exceptions. Not a blocker tho.
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.
Yes, you can use Applicative
for this.
Validated.applicative(NonEmptyList.semigroup<String>()).tupledN(
1.valid(),
"2".invalidNel(),
"3".invalidNel()
) // Validated.Invalid(NonEmptyList("2", "3"))
You can always make these exhaustive by nesting when
, which after a lot of back and forth have gone back to...
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.
Right, I just realized that we're in core-data and we don't have access to the extensions :/
What I did is the following:
- Added a convenient
nelApplicative
function in Validated extensions - Suggest to use this approach instead of the custom function in docs, but without changing the examples code due to the import problem
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 just meh in general. It could be possible to rewrite it as two nested folds and some manual formatting.
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 not a fan of showing parallelValidate
at all.. It promotes/shows code that no-one should ever write using Arrow.
We have a bunch of helpers
in the docs
module, any way we can provide instances there to make the desired snippets compile?
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.
The only reason I like it is for the explanation vs a Validated Nel, which is also explained in there. I changed it to try to make it clear that this is just for the shake of the example
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.
Looks great!
We should refactor those snippets to use mapN
or tupledN
instead instead of parallelValidate
before we merge.
@pakoito Would you take a second look at the docs when you have time, please? 🙏😁 |
* } | ||
* is None -> Validated.Invalid(ConfigError.MissingConfig(key)) | ||
* suspend fun <A> parse(read: Read<A>, key: String) = Validated.fx<ConfigError, A> { | ||
* val value = Validated.fromNullable(map[key]) { |
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.
Neat
* val value = Validated.fromNullable(map[key]) { | ||
* ConfigError.MissingConfig(key) | ||
* }.bind() | ||
* val readVal = Validated.fromOption(read.read(value)) { |
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.
Great! Good use of both APIs
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 was being extra nitpicky on the review. Nothing's a blocker really.
…ore into ab/validated-fx-suspend
Language Review
@nomisRev If you have any more comments later please let me know so I can open another PR :) |
Done in this PR
Details
As per #116 this PR adds fx to validated, so one simple example can be found in the docs of validated itself, where we have the following code:
So, instead of dealing with option, I added a
Validated.fromNullable
to matchfromOption
already present and simply deal with validates in an fx block:The only difference is that we force
parse
to be suspended because of this version of fx being suspended (shall we wait for #124?).