-
Notifications
You must be signed in to change notification settings - Fork 53
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
Support optionalSetTo
or setToOptional
#23
Comments
Speaking of this, I was also wondering if the following feature would be useful: // map
Person(35).modify(_.age).setToM(List(20, 28)) shouldEqual
List(Person(20), Person(28))
// flatMap
List(Person(35)).modify(_.each.age).setToM(List(20, 28)) shouldEqual
List(Person(20), Person(28)) I will probably implement it and send a PR if I like it. |
Maybe it would be enough to locally pimp the |
Monadic modification is really useful, for example, given a val genValidPerson: Gen[Person] =
Gen.const(Person("John", 30))
val genInvalidPersonAge: Gen[Person] =
genValidPerson.modify(_.each.age).setToM(Gen.oneOf(-1, 0))
forAll(genInvalidPersonAge) { person =>
// should be invalid
} |
Of course this can also be added on top in other ways, e.g. using monadic workflows: val genValidPerson: Gen[Person] =
Gen.const(Person("John", 30))
val genInvalidPersonAge: Gen[Person] = monadic[Gen] {
genValidPerson.each.modify(_.age).setTo(Gen.oneOf(-1, 0).each)
}
forAll(genInvalidPersonAge) { person =>
// should be invalid
} But I think it does not work with closures, so it wouldn’t be possible to emulate |
But that would add a dependency on scalaz's/cat's/... |
Though I really like the example with quickcheck :) |
IMHO scalaz/cats will add too much weight to this library. The idea was to add |
Definitely, adding scalaz/cats dependency would be way too much :) I'm not at all happy having to define yet another Maybe it would be good to document the approach instead of coding? |
This seems fair, although the said functionality might as well be a library. Would you be interested in a PR with that as a separate module (“project” in SBT terms)? |
Sure, a separate module sounds good as well :) |
I think it would be useful to have a version of
setTo
which accepts anOption
and only changes the object if the value being passed in isSome
.The text was updated successfully, but these errors were encountered: