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 more ensure functions #22

Merged
merged 3 commits into from
Jun 5, 2023
Merged

Add more ensure functions #22

merged 3 commits into from
Jun 5, 2023

Conversation

ustitc
Copy link
Collaborator

@ustitc ustitc commented Jun 3, 2023

  • Added ensure(condition: Boolean) for simple use-cases:
companion object : Exact<String, NotBlankString> by Exact({
   ensure(it.isNotBlank()) // use default ExactError in case of error
   NotBlankString(it)
})
  • Renamed to ensureExact because it conflicted with existing ensure from core. Exact is a functional interface and ide struggles to understand what function to use, ensure(boolean, () -> Error) or ensure(boolean, Exact<Boolean, ...>):
companion object : Exact<Int, ID> by Exact({
  ensure(it > 0) { ExactError("Some error") } // arrow.exact.ensure is used instead of arrow.core.raise.ensure
  ID(it)
})
  • Added more ensureExact functions for composing Exacts
  • Explicitly define all combination of ensureExact instead of using defaults for better experience in ide:
image

@ILIYANGERMANOV
Copy link
Collaborator

@ustits looks good. I just don't understand what's the value of having an Exact's ensure and not simply re-using the Raise's one?

@ustitc
Copy link
Collaborator Author

ustitc commented Jun 3, 2023

@ILIYANGERMANOV the main reason is to be able to reuse Exacts. I have such an example:

@JvmInline
value class Name private constructor(private val value: String) {

    companion object : ExactEither<NameError, String, Name> by ExactEither({
        ensure(it.isNotBlank()) { BlankName }
        ensure(it.length <= 32) { LargeName }
        Name(it)
    })
}

And I have Currency class which has the same logic for validation. With help of ensureExact instead of copy-pasting I can reuse Name:

@JvmInline
value class Currency private constructor(private val name: String) {

    companion object : Exact<String, Currency> by Exact({
        ensureExact(it, Name)
        Currency(it)
    })
}

Copy link
Collaborator

@ILIYANGERMANOV ILIYANGERMANOV left a comment

Choose a reason for hiding this comment

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

Makes sense - thanks for the example! I'm wondering if we can make ensureExact(it, Name) nicer? But I don't have any bright ideas atm. Good addition to the library 👏

@nomisRev nomisRev merged commit 20d3892 into main Jun 5, 2023
4 checks passed
@nomisRev nomisRev deleted the add_ensures branch June 5, 2023 11:42
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.

None yet

3 participants