Scalactic Or in for compresension with 'if' guard

5 views
Skip to first unread message

Joost den Boer

unread,
Aug 29, 2016, 5:34:34 AM8/29/16
to scalatest-users
I'm trying to use an Or in a for-compresension with an 'if' guard, but this gives me a

`type mismatch; found: Boolean, required: org.scalactic.Validation[?]` compile error.

code example
```
type Result = String or Int
def someFunctionWithResult: Result

for {
  or <- someFunctionWithResult
  if (or == "ok?")
} yield or
```

Am I doing it wrong or is it just because the 'Or' only has a 'filter' method which takes a 'Validation' instance.
I already have a 'predicateAsValidation' function, but this is not usable in the for-comprehension context.
```
def predicateAsValidation[A](predicate: A => Boolean)(a: A): Validation[One[ErrorMessage]] =
if (predicate(a)) Pass else Fail(One(FilterMismatchError(a.toString)))
```
Reply all
Reply to author
Forward
0 new messages