Converts this Or
to an Or
with the same Good
type and a Bad
type consisting of
One
parameterized by this Or
's Bad
type.
Converts this Or
to an Or
with the same Good
type and a Bad
type consisting of
One
parameterized by this Or
's Bad
type.
For example, invoking the accumulating
method on an Int Or ErrorMessage
would convert it to an
Int Or One[ErrorMessage]
. This result type, because the Bad
type is an Every
, can be used
with the mechanisms provided in trait Accumulation
to accumulate errors.
Note that if this Or
is already an accumulating Or
, the behavior of this accumulating
method does not change.
For example, if you invoke accumulating
on an Int Or One[ErrorMessage]
you will be rewarded with an
Int Or One[One[ErrorMessage]]
.
this Good
, if this Or
is a Good
; or this Bad
value wrapped in a One
if
this Or
is a Bad
.
Returns this Good
with the type widened to Or
.
Returns this Good
with the type widened to Or
.
This widening method can useful when the compiler infers the more specific Good
type and
you need the more general Or
type. Here's an example that uses foldLeft
on
a List[Int]
to calculate a sum, so long as only odd numbers exist in a passed List
:
scala> import org.scalactic._ import org.scalactic._scala> def oddSum(xs: List[Int]): Int Or ErrorMessage = | xs.foldLeft(Good(0).orBad[ErrorMessage]) { (acc, x) => | acc match { | case Bad(_) => acc | case Good(_) if (x % 2 == 0) => Bad(x + " is not odd") | case Good(sum) => Good(sum + x) | } | } <console>:13: error: constructor cannot be instantiated to expected type; found : org.scalactic.Bad[G,B] required: org.scalactic.Good[Int,org.scalactic.ErrorMessage] case Bad(_) => acc <console>:14: error: type mismatch; found : org.scalactic.Bad[Nothing,String] required: org.scalactic.Good[Int,org.scalactic.ErrorMessage] case Good(_) if (x % 2 == 0) => Bad(x + " is not odd")
Because the compiler infers the type of the first parameter to foldLeft
to be Good[Int, ErrorMessage]
,
it expects the same type to appear as the result type of function passed as the second, curried parameter. What you really want is
that both types be Int Or ErrorMessage
, but the compiler thinks you want them to be the more specific
Good[Int, ErrorMessage]
. You can use the asOr
method to indicate you want the type to be Or
with minimal boilerplate:
scala> def oddSum(xs: List[Int]): Int Or ErrorMessage = | xs.foldLeft(Good(0).orBad[ErrorMessage].asOr) { (acc, x) => | acc match { | case Bad(_) => acc | case Good(_) if (x % 2 == 0) => Bad(x + " is not odd") | case Good(sum) => Good(sum + x) | } | } oddSum: (xs: List[Int])org.scalactic.Or[Int,org.scalactic.ErrorMessage]
Now you can use the method to sum a List
of odd numbers:
scala> oddSum(List(1, 2, 3)) res2: org.scalactic.Or[Int,org.scalactic.ErrorMessage] = Bad(2 is not odd)scala> oddSum(List(1, 3, 5)) res3: org.scalactic.Or[Int,org.scalactic.ErrorMessage] = Good(9)
Maps the given function to this Or
's value if it is a Bad
or returns this
if it is a Good
.
Returns true
if this Or
is a Good
and the predicate p
returns true when applied to this Good
's value.
Returns true
if this Or
is a Good
and the predicate p
returns true when applied to this Good
's value.
Note: The exists
method will return the same result as forall
if this Or
is a Good
, but the opposite
result if this Or
is a Bad
.
the predicate to apply to the Good
value, if this is a Good
the result of applying the passed predicate p
to the Good
value, if this is a Good
, else false
Returns this Or
if either 1) it is a Bad
or 2) it is a Good
and applying the validation function f
to this
Good
's value returns Pass
; otherwise,
returns a new Bad
containing the error value contained in the Fail
resulting from applying the validation
function f
to this Good
's value.
Returns this Or
if either 1) it is a Bad
or 2) it is a Good
and applying the validation function f
to this
Good
's value returns Pass
; otherwise,
returns a new Bad
containing the error value contained in the Fail
resulting from applying the validation
function f
to this Good
's value.
For examples of filter
used in for
expressions, see the main documentation for trait
Validation
.
the validation function to apply
a Good
if this Or
is a Good
that passes the validation function, else a Bad
.
Returns the given function applied to the value contained in this Or
if it is a Good
,
or returns this
if it is a Bad
.
Returns the given function applied to the value contained in this Or
if it is a Good
,
or returns this
if it is a Bad
.
the function to apply
if this is a Good
, the result of applying the given function to the contained value wrapped in a Good
,
else this Bad
is returned
Folds this Or
into a value of type V
by applying the given gf
function if this is
a Good
else the given bf
function if this is a Bad
.
Folds this Or
into a value of type V
by applying the given gf
function if this is
a Good
else the given bf
function if this is a Bad
.
the function to apply to this Or
's Good
value, if it is a Good
the function to apply to this Or
's Bad
value, if it is a Bad
the result of applying the appropriate one of the two passed functions, gf
or bf, to this Or
's value
Returns true
if either this Or
is a Bad
or if the predicate p
returns true
when applied
to this Good
's value.
Returns true
if either this Or
is a Bad
or if the predicate p
returns true
when applied
to this Good
's value.
Note: The forall
method will return the same result as exists
if this Or
is a Good
, but the opposite
result if this Or
is a Bad
.
the predicate to apply to the Good
value, if this is a Good
the result of applying the passed predicate p
to the Good
value, if this is a Good
, else true
Applies the given function f to the contained value if this Or
is a Good
; does nothing if this Or
is a Bad
.
the “good” value ”“
the “good” value
Returns the Or
's value if it is a Good
or throws NoSuchElementException
if it is a Bad
.
Returns, if this Or
is Good
, this Good
's value; otherwise returns the result of evaluating default
.
Indicates whether this Or
is a Bad
Indicates whether this Or
is a Bad
true if this Or
is a Bad
, false
if it is a Good
.
Indicates whether this Or
is a Good
Maps the given function to this Or
's value if it is a Good
or returns this
if it is a Bad
.
Narrows the Bad
type of this Good
to the given type.
Narrows the Bad
type of this Good
to the given type.
Because Or
has two types, but the Good
factory method only takes a value of the “good” type, the Scala compiler will
infer Nothing
for the Bad
type:
scala> Good(3) res0: org.scalactic.Good[Int,Nothing] = Good(3)
Often Nothing
will work fine, as it will be widened as soon as the compiler encounters a more specific Bad
type.
Sometimes, however, you may need to specify it. In such situations you can use this orBad
method, like this:
scala> Good(3).orBad[String] res1: org.scalactic.Good[Int,String] = Good(3)
Returns this Or
if it is a Good
, otherwise returns the result of evaluating the passed alternative
.
Maps the given function to this Or
's value if it is a Bad
, transforming it into a Good
, or returns
this
if it is already a Good
.
Maps the given function to this Or
's value if it is a Bad
, transforming it into a Good
, or returns
this
if it is already a Good
.
the function to apply
if this is a Bad
, the result of applying the given function to the contained value wrapped in a Good
,
else this Good
is returned
Maps the given function to this Or
's value if it is a Bad
, returning the result, or returns
this
if it is already a Good
.
Returns an Or
with the Good
and Bad
types swapped: Bad
becomes Good
and Good
becomes Bad
.
Returns an Or
with the Good
and Bad
types swapped: Bad
becomes Good
and Good
becomes Bad
.
Here's an example:
scala> val lyrics = Bad("Hey Jude, don't make it bad. Take a sad song and make it better.") lyrics: org.scalactic.Bad[Nothing,String] = Bad(Hey Jude, don't make it bad. Take a sad song and make it better.)scala> lyrics.swap res12: org.scalactic.Or[String,Nothing] = Good(Hey Jude, don't make it bad. Take a sad song and make it better.)
Now that song will be rolling around in your head all afternoon. But at least it is a good song (thanks to swap
).
if this Or
is a Good
, its Good
value wrapped in a Bad
; if this Or
is
a Bad
, its Bad
value wrapped in a Good
.
Returns an Either
: a Right
containing the Good
value, if this is a Good
; a Left
containing the Bad
value, if this is a Bad
.
Returns an Either
: a Right
containing the Good
value, if this is a Good
; a Left
containing the Bad
value, if this is a Bad
.
Note that values effectively “switch sides” when convering an Or
to an Either
. If the type of the
Or
on which you invoke toEither
is Or[Int, ErrorMessage]
for example, the result will be an
Either[ErrorMessage, Int]
. The reason is that the convention for Either
is that Left
is used for “bad”
values and Right
is used for “good” ones.
this Good
value, wrapped in a Right
, or this Bad
value, wrapped in a Left
.
Returns a Some
containing the Good
value, if this Or
is a Good
, else None
.
Returns an immutable IndexedSeq
containing the Good
value, if this Or
is a Good
, else an empty
immutable IndexedSeq
.
Returns a Try
: a Success
containing the
Good
value, if this is a Good
; a Failure
containing the Bad
value, if this is a Bad
.
Returns a Try
: a Success
containing the
Good
value, if this is a Good
; a Failure
containing the Bad
value, if this is a Bad
.
Note: This method can only be called if the Bad
type of this Or
is a subclass
of Throwable
(or Throwable
itself).
Note that values effectively “switch sides” when converting an Or
to an Either
. If the type of the
Or
on which you invoke toEither
is Or[Int, ErrorMessage]
for example, the result will be an
Either[ErrorMessage, Int]
. The reason is that the convention for Either
is that Left
is used for “bad”
values and Right
is used for “good” ones.
this Good
value, wrapped in a Right
, or this Bad
value, wrapped in a Left
.
Transforms this Or
by applying the function gf
to this Or
's Good
value if it is a Good
,
or by applying bf
to this Or
's Bad
value if it is a Bad
.
Transforms this Or
by applying the function gf
to this Or
's Good
value if it is a Good
,
or by applying bf
to this Or
's Bad
value if it is a Bad
.
the function to apply to this Or
's Good
value, if it is a Good
the function to apply to this Or
's Bad
value, if it is a Bad
the result of applying the appropriate one of the two passed functions, gf
or bf, to this Or
's value
Currently just forwards to filter, and therefore, returns the same result.
Currently just forwards to filter, and therefore, returns the same result.
Contains a “good” value.
You can decide what “good” means, but it is expected
Good
will be commonly used to hold valid results for processes that may fail with an error instead of producing a valid result.the “good” value