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

Combining boolean predicates #917

Closed
matwojcik opened this issue Mar 5, 2021 · 5 comments
Closed

Combining boolean predicates #917

matwojcik opened this issue Mar 5, 2021 · 5 comments

Comments

@matwojcik
Copy link
Contributor

I have some problem when playing with refines using boolean predicates.

  object test {
    import eu.timepit.refined.auto._
    type LongOrDouble = NonEmpty And (ValidLong Or ValidDouble)

    val SomeLong: String Refined (NonEmpty And ValidLong) = "11"
    val SomeLongOrDouble: String Refined LongOrDouble = SomeLong


  }

which fails with

[error] type mismatch;
[error]  found   : eu.timepit.refined.api.Refined[String,eu.timepit.refined.boolean.And[eu.timepit.refined.collection.NonEmpty,eu.timepit.refined.string.ValidLong]]
[error]     (which expands to)  eu.timepit.refined.api.Refined[String,eu.timepit.refined.boolean.And[eu.timepit.refined.boolean.Not[eu.timepit.refined.collection.Empty],eu.timepit.refined.string.ValidLong]]
[error]  required: eu.timepit.refined.api.Refined[String,test.LongOrDouble]
[error]     (which expands to)  eu.timepit.refined.api.Refined[String,eu.timepit.refined.boolean.And[eu.timepit.refined.boolean.Not[eu.timepit.refined.collection.Empty],eu.timepit.refined.boolean.Or[eu.timepit.refined.string.ValidLong,eu.timepit.refined.string.ValidDouble]]]
[error]   val SomeLongOrDouble: String Refined LongOrDouble = SomeLong
[error]                                                       ^

object test2 {
  import eu.timepit.refined.auto._
  type LongOrDouble = (NonEmpty And ValidLong) Or (NonEmpty And ValidDouble)

  val SomeLongOrDouble: String Refined LongOrDouble = "11"

  val SomeNonEmpty: String Refined NonEmpty = SomeLongOrDouble
}

which fails with

[error] type mismatch;
[error]  found   : eu.timepit.refined.api.Refined[String,test2.LongOrDouble]
[error]     (which expands to)  eu.timepit.refined.api.Refined[String,eu.timepit.refined.boolean.Or[eu.timepit.refined.boolean.And[eu.timepit.refined.boolean.Not[eu.timepit.refined.collection.Empty],eu.timepit.refined.string.ValidLong],eu.timepit.refined.boolean.And[eu.timepit.refined.boolean.Not[eu.timepit.refined.collection.Empty],eu.timepit.refined.string.ValidDouble]]]
[error]  required: eu.timepit.refined.api.Refined[String,eu.timepit.refined.collection.NonEmpty]
[error]     (which expands to)  eu.timepit.refined.api.Refined[String,eu.timepit.refined.boolean.Not[eu.timepit.refined.collection.Empty]]
[error]   val SomeNonEmpty: String Refined NonEmpty = SomeLongOrDouble
[error]                                  

I'm not sure if this is limitation of scala as a language or is there some possibility for that to work?

@matwojcik
Copy link
Contributor Author

This does not resolve the issue I described, but pretty much resolves the initial issue I had - is there any way to imply that if a String is a ValidLong then it must be NonEmpty by the long definition?

@kubukoz
Copy link
Contributor

kubukoz commented Mar 5, 2021

Looks like an Inference (https://github.com/fthomas/refined/blob/b9b027b4f3a42409b656a2331bfccdff1c922751/modules/core/shared/src/main/scala/eu/timepit/refined/api/Inference.scala) instance is missing for A and B ==> A / A and B ==> B 🤔

edit: it's more complicated but something related to these instances, probably

kubukoz added a commit to kubukoz/refined that referenced this issue Mar 5, 2021
@matwojcik
Copy link
Contributor Author

matwojcik commented Mar 6, 2021

@kubukoz nice! Is this mechanism also a good fit for ValidLong ==> NonEmpty so my original issue, which lead me to this?

@kubukoz
Copy link
Contributor

kubukoz commented Mar 6, 2021

I think so. :)

@fthomas
Copy link
Owner

fthomas commented May 19, 2021

Fixed by #927.

@fthomas fthomas closed this as completed May 19, 2021
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 a pull request may close this issue.

3 participants