final case class Good[+G](g: G) extends Or[G, Nothing] with Product with Serializable
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.
- g
the “good” value
- Source
- Or.scala
- Alphabetic
- By Inheritance
- Good
- Or
- Serializable
- Serializable
- Product
- Equals
- AnyRef
- Any
- Hide All
- Show All
- Public
- All
Instance Constructors
-
new
Good(g: G)
- g
the “good” value
Value Members
-
final
def
!=(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
-
final
def
##(): Int
- Definition Classes
- AnyRef → Any
-
final
def
==(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
-
def
accumulating: Or[G, One[Nothing]]
Converts this
Or
to anOr
with the sameGood
type and aBad
type consisting ofOne
parameterized by thisOr
'sBad
type.Converts this
Or
to anOr
with the sameGood
type and aBad
type consisting ofOne
parameterized by thisOr
'sBad
type.For example, invoking the
accumulating
method on anInt Or ErrorMessage
would convert it to anInt Or One[ErrorMessage]
. This result type, because theBad
type is anEvery
, can be used with the mechanisms provided in traitAccumulation
to accumulate errors.Note that if this
Or
is already an accumulatingOr
, the behavior of thisaccumulating
method does not change. For example, if you invokeaccumulating
on anInt Or One[ErrorMessage]
you will be rewarded with anInt Or One[One[ErrorMessage]]
.- returns
this
Good
, if thisOr
is aGood
; or thisBad
value wrapped in aOne
if thisOr
is aBad
.
-
final
def
asInstanceOf[T0]: T0
- Definition Classes
- Any
-
def
badMap[C](f: (Nothing) ⇒ C): Or[G, C]
Maps the given function to this
Or
's value if it is aBad
or returnsthis
if it is aGood
. -
def
clone(): AnyRef
- Attributes
- protected[java.lang]
- Definition Classes
- AnyRef
- Annotations
- @native() @throws( ... )
-
final
def
eq(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
-
def
exists(p: (G) ⇒ Boolean): Boolean
Returns
true
if thisOr
is aGood
and the predicatep
returns true when applied to thisGood
's value.Returns
true
if thisOr
is aGood
and the predicatep
returns true when applied to thisGood
's value.Note: The
exists
method will return the same result asforall
if thisOr
is aGood
, but the opposite result if thisOr
is aBad
.- p
the predicate to apply to the
Good
value, if this is aGood
- returns
the result of applying the passed predicate
p
to theGood
value, if this is aGood
, elsefalse
-
def
filter[C](f: (G) ⇒ Validation[C]): Or[G, C]
Returns this
Or
if either 1) it is aBad
or 2) it is aGood
and applying the validation functionf
to thisGood
's value returnsPass
; otherwise, returns a newBad
containing the error value contained in theFail
resulting from applying the validation functionf
to thisGood
's value.Returns this
Or
if either 1) it is aBad
or 2) it is aGood
and applying the validation functionf
to thisGood
's value returnsPass
; otherwise, returns a newBad
containing the error value contained in theFail
resulting from applying the validation functionf
to thisGood
's value.For examples of
filter
used infor
expressions, see the main documentation for traitValidation
.- f
the validation function to apply
- returns
a
Good
if thisOr
is aGood
that passes the validation function, else aBad
.
-
def
finalize(): Unit
- Attributes
- protected[java.lang]
- Definition Classes
- AnyRef
- Annotations
- @throws( classOf[java.lang.Throwable] )
-
def
flatMap[H, C](f: (G) ⇒ Or[H, C]): Or[H, C]
Returns the given function applied to the value contained in this
Or
if it is aGood
, or returnsthis
if it is aBad
.Returns the given function applied to the value contained in this
Or
if it is aGood
, or returnsthis
if it is aBad
.- f
the function to apply
- returns
if this is a
Good
, the result of applying the given function to the contained value wrapped in aGood
, else thisBad
is returned
-
def
fold[V](gf: (G) ⇒ V, bf: (Nothing) ⇒ V): V
Folds this
Or
into a value of typeV
by applying the givengf
function if this is aGood
else the givenbf
function if this is aBad
.Folds this
Or
into a value of typeV
by applying the givengf
function if this is aGood
else the givenbf
function if this is aBad
.- gf
the function to apply to this
Or
'sGood
value, if it is aGood
- bf
the function to apply to this
Or
'sBad
value, if it is aBad
- returns
the result of applying the appropriate one of the two passed functions,
gf
or bf, to thisOr
's value
-
def
forall(p: (G) ⇒ Boolean): Boolean
Returns
true
if either thisOr
is aBad
or if the predicatep
returnstrue
when applied to thisGood
's value.Returns
true
if either thisOr
is aBad
or if the predicatep
returnstrue
when applied to thisGood
's value.Note: The
forall
method will return the same result asexists
if thisOr
is aGood
, but the opposite result if thisOr
is aBad
.- p
the predicate to apply to the
Good
value, if this is aGood
- returns
the result of applying the passed predicate
p
to theGood
value, if this is aGood
, elsetrue
-
def
foreach(f: (G) ⇒ Unit): Unit
Applies the given function f to the contained value if this
Or
is aGood
; does nothing if thisOr
is aBad
. - val g: G
-
def
get: G
Returns the
Or
's value if it is aGood
or throwsNoSuchElementException
if it is aBad
. -
final
def
getClass(): Class[_]
- Definition Classes
- AnyRef → Any
- Annotations
- @native()
-
def
getOrElse[H >: G](default: ⇒ H): G
Returns, if this
Or
isGood
, thisGood
's value; otherwise returns the result of evaluatingdefault
. -
val
isBad: Boolean
Indicates whether this
Or
is aBad
Indicates whether this
Or
is aBad
- returns
true if this
Or
is aBad
,false
if it is aGood
.
- Definition Classes
- Or
- val isGood: Boolean
-
final
def
isInstanceOf[T0]: Boolean
- Definition Classes
- Any
-
def
map[H](f: (G) ⇒ H): Or[H, Nothing]
Maps the given function to this
Or
's value if it is aGood
or returnsthis
if it is aBad
. -
final
def
ne(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
-
final
def
notify(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native()
-
final
def
notifyAll(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native()
-
def
orBad[C]: Or[G, C]
Narrows the
Bad
type of thisGood
to the given type.Narrows the
Bad
type of thisGood
to the given type.Because
Or
has two types, but theGood
factory method only takes a value of the “good” type, the Scala compiler will inferNothing
for theBad
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 specificBad
type. Sometimes, however, you may need to specify it. In such situations you can use thisorBad
method, like this:scala> Good(3).orBad[String] res1: org.scalactic.Good[Int,String] = Good(3)
-
def
orElse[H >: G, C](alternative: ⇒ Or[H, C]): Or[G, Nothing]
Returns this
Or
if it is aGood
, otherwise returns the result of evaluating the passedalternative
. -
def
recover[H >: G](f: (Nothing) ⇒ H): Or[H, Nothing]
Maps the given function to this
Or
's value if it is aBad
, transforming it into aGood
, or returnsthis
if it is already aGood
.Maps the given function to this
Or
's value if it is aBad
, transforming it into aGood
, or returnsthis
if it is already aGood
.- f
the function to apply
- returns
if this is a
Bad
, the result of applying the given function to the contained value wrapped in aGood
, else thisGood
is returned
-
def
recoverWith[H >: G, C](f: (Nothing) ⇒ Or[H, C]): Or[H, C]
Maps the given function to this
Or
's value if it is aBad
, returning the result, or returnsthis
if it is already aGood
. -
def
swap: Or[Nothing, G]
Returns an
Or
with theGood
andBad
types swapped:Bad
becomesGood
andGood
becomesBad
.Returns an
Or
with theGood
andBad
types swapped:Bad
becomesGood
andGood
becomesBad
.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
).- returns
if this
Or
is aGood
, itsGood
value wrapped in aBad
; if thisOr
is aBad
, itsBad
value wrapped in aGood
.
-
final
def
synchronized[T0](arg0: ⇒ T0): T0
- Definition Classes
- AnyRef
-
def
toEither: Either[Nothing, G]
Returns an
Either
: aRight
containing theGood
value, if this is aGood
; aLeft
containing theBad
value, if this is aBad
.Returns an
Either
: aRight
containing theGood
value, if this is aGood
; aLeft
containing theBad
value, if this is aBad
.Note that values effectively “switch sides” when convering an
Or
to anEither
. If the type of theOr
on which you invoketoEither
isOr[Int, ErrorMessage]
for example, the result will be anEither[ErrorMessage, Int]
. The reason is that the convention forEither
is thatLeft
is used for “bad” values andRight
is used for “good” ones.- returns
this
Good
value, wrapped in aRight
, or thisBad
value, wrapped in aLeft
.
-
def
toOption: Some[G]
Returns a
Some
containing theGood
value, if thisOr
is aGood
, elseNone
. -
def
toSeq: IndexedSeq[G]
Returns an immutable
IndexedSeq
containing theGood
value, if thisOr
is aGood
, else an empty immutableIndexedSeq
. -
def
toTry(implicit ev: <:<[Nothing, Throwable]): Success[G]
Returns a
Try
: aSuccess
containing theGood
value, if this is aGood
; aFailure
containing theBad
value, if this is aBad
.Returns a
Try
: aSuccess
containing theGood
value, if this is aGood
; aFailure
containing theBad
value, if this is aBad
.Note: This method can only be called if the
Bad
type of thisOr
is a subclass ofThrowable
(orThrowable
itself).Note that values effectively “switch sides” when converting an
Or
to anEither
. If the type of theOr
on which you invoketoEither
isOr[Int, ErrorMessage]
for example, the result will be anEither[ErrorMessage, Int]
. The reason is that the convention forEither
is thatLeft
is used for “bad” values andRight
is used for “good” ones.- returns
this
Good
value, wrapped in aRight
, or thisBad
value, wrapped in aLeft
.
-
def
transform[H, C](gf: (G) ⇒ Or[H, C], bf: (Nothing) ⇒ Or[H, C]): Or[H, C]
Transforms this
Or
by applying the functiongf
to thisOr
'sGood
value if it is aGood
, or by applyingbf
to thisOr
'sBad
value if it is aBad
.Transforms this
Or
by applying the functiongf
to thisOr
'sGood
value if it is aGood
, or by applyingbf
to thisOr
'sBad
value if it is aBad
.- gf
the function to apply to this
Or
'sGood
value, if it is aGood
- bf
the function to apply to this
Or
'sBad
value, if it is aBad
- returns
the result of applying the appropriate one of the two passed functions,
gf
or bf, to thisOr
's value
-
final
def
wait(): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws( ... )
-
final
def
wait(arg0: Long, arg1: Int): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws( ... )
-
final
def
wait(arg0: Long): Unit
- Definition Classes
- AnyRef
- Annotations
- @native() @throws( ... )
-
def
withFilter[C >: Nothing](f: (G) ⇒ Validation[C]): Or[G, C]
Currently just forwards to filter, and therefore, returns the same result.
Currently just forwards to filter, and therefore, returns the same result.
- Definition Classes
- Or
Deprecated Value Members
-
def
asOr: Or[G, Nothing]
The
asOr
method has been deprecated and will be removed in a future version of Scalactic. Please remove invocations ofasOr
in expressions of typeGood(value).orBad[Type]
andGood[Type].orBad(value)
(which now return a type already widened toOr
), otherwise please use a type annotation to widen the type, such as:(Good(3): Int Or ErrorMessage)
.The
asOr
method has been deprecated and will be removed in a future version of Scalactic. Please remove invocations ofasOr
in expressions of typeGood(value).orBad[Type]
andGood[Type].orBad(value)
(which now return a type already widened toOr
), otherwise please use a type annotation to widen the type, such as:(Good(3): Int Or ErrorMessage)
.- Definition Classes
- Good → Or
- Annotations
- @deprecated
- Deprecated
The asOr is no longer needed because Good(value).orBad[Type] and Good[Type].orBad(value) now return Or. You can delete invocations of asOr in those cases, otherwise, please use a type annotation to widen the type, like (Good(3): Int Or ErrorMessage).