org.scalautils

Bad

final case class Bad[+G, +B](b: B) extends Or[G, B] with Product with Serializable

Contains a “bad” value.

You can decide what “bad” means, but it is expected Bad will be commonly used to hold descriptions of an error (or several, accumulated errors). Some examples of possible error descriptions are String error messages, Int error codes, Throwable exceptions, or instances of a case class hierarchy designed to describe errors.

b

the “bad” value

Source
Or.scala
Linear Supertypes
Serializable, Serializable, Product, Equals, Or[G, B], AnyRef, Any
Ordering
  1. Alphabetic
  2. By inheritance
Inherited
  1. Bad
  2. Serializable
  3. Serializable
  4. Product
  5. Equals
  6. Or
  7. AnyRef
  8. Any
  1. Hide All
  2. Show all
Learn more about member selection
Visibility
  1. Public
  2. All

Instance Constructors

  1. new Bad(b: B)

    b

    the “bad” value

Value Members

  1. final def !=(arg0: AnyRef): Boolean

    Definition Classes
    AnyRef
  2. final def !=(arg0: Any): Boolean

    Definition Classes
    Any
  3. final def ##(): Int

    Definition Classes
    AnyRef → Any
  4. final def ==(arg0: AnyRef): Boolean

    Definition Classes
    AnyRef
  5. final def ==(arg0: Any): Boolean

    Definition Classes
    Any
  6. def accumulating: Or[G, One[B]]

    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]].

    returns

    this Good, if this Or is a Good; or this Bad value wrapped in a One if this Or is a Bad.

    Definition Classes
    BadOr
  7. final def asInstanceOf[T0]: T0

    Definition Classes
    Any
  8. def asOr: Or[G, B]

    Returns this Bad with the type widened to Or.

    Returns this Bad with the type widened to Or.

    This widening method can useful when the compiler infers the more specific Bad type and you need the more general Or type. Here's an example that uses foldLeft on a List[Int] to find the first even number in the List:

    scala> import org.scalautils._
    import org.scalautils._

    scala> def findFirstEven(xs: List[Int]): Int Or ErrorMessage = | xs.foldLeft(Good[Int].orBad("No even nums")) { (acc, x) => | acc orElse (if (x % 2 == 0) Good(x) else acc) | } <console>:13: error: type mismatch; found : org.scalautils.Or[Int,String] required: org.scalautils.Bad[Int,String] acc orElse (if (x % 2 == 0) Good(x) else acc) ^

    Because the compiler infers the type of the first parameter to foldLeft to be Bad[Int, String], 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 String, but the compiler thinks you want them to be the more specific Bad[Int, String]. You can use the asOr method to indicate you want the type to be Or with minimal boilerplate:

    scala> def findFirstEven(xs: List[Int]): Int Or ErrorMessage =
         |   xs.foldLeft(Good[Int].orBad("No even nums").asOr) { (acc, x) =>
         |     acc orElse (if (x % 2 == 0) Good(x) else acc)
         |   }
    findFirstEven: (xs: List[Int])org.scalautils.Or[Int,String]
    

    Now you can use the method to find the first even number in a List:

    scala> findFirstEven(List(1, 2, 3))
    res4: org.scalautils.Or[Int,ErrorMessage] = Good(2)

    scala> findFirstEven(List(1, 3, 5)) res5: org.scalautils.Or[Int,ErrorMessage] = Bad(No even nums)

  9. val b: B

    the “bad” value

    the “bad” value

  10. def badMap[C](f: (B) ⇒ C): Or[G, C]

    Maps the given function to this Or's value if it is a Bad or returns this if it is a Good.

    Maps the given function to this Or's value if it is a Bad or returns this if it is a Good.

    f

    the function to apply

    returns

    if this is a Bad, the result of applying the given function to the contained value wrapped in a Bad, else this Good is returned

    Definition Classes
    BadOr
  11. def clone(): AnyRef

    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws()
  12. final def eq(arg0: AnyRef): Boolean

    Definition Classes
    AnyRef
  13. def exists(p: (G) ⇒ Boolean): Boolean

    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.

    p

    the predicate to apply to the Good value, if this is a Good

    returns

    the result of applying the passed predicate p to the Good value, if this is a Good, else false

    Definition Classes
    BadOr
  14. def filter[C >: B](f: (G) ⇒ Validation[C]): Or[G, C]

    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.

    f

    the validation function to apply

    returns

    a Good if this Or is a Good that passes the validation function, else a Bad.

    Definition Classes
    BadOr
  15. def finalize(): Unit

    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws()
  16. def flatMap[H, C >: B](f: (G) ⇒ Or[H, C]): Or[H, C]

    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.

    f

    the function to apply

    returns

    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

    Definition Classes
    BadOr
  17. def fold[V](gf: (G) ⇒ V, bf: (B) ⇒ V): V

    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.

    gf

    the function to apply to this Or's Good value, if it is a Good

    bf

    the function to apply to this Or's Bad value, if it is a Bad

    returns

    the result of applying the appropriate one of the two passed functions, gf or bf, to this Or's value

    Definition Classes
    BadOr
  18. def forall(p: (G) ⇒ Boolean): Boolean

    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.

    p

    the predicate to apply to the Good value, if this is a Good

    returns

    the result of applying the passed predicate p to the Good value, if this is a Good, else true

    Definition Classes
    BadOr
  19. def foreach(f: (G) ⇒ Unit): Unit

    Applies the given function f to the contained value if this Or is a Good; does nothing if this Or is a Bad.

    Applies the given function f to the contained value if this Or is a Good; does nothing if this Or is a Bad.

    f

    the function to apply

    Definition Classes
    BadOr
  20. def get: G

    Returns the Or's value if it is a Good or throws NoSuchElementException if it is a Bad.

    Returns the Or's value if it is a Good or throws NoSuchElementException if it is a Bad.

    returns

    the contained value if this is a Good

    Definition Classes
    BadOr
    Exceptions thrown
    NoSuchElementException

    if this is a Bad

  21. final def getClass(): Class[_]

    Definition Classes
    AnyRef → Any
  22. def getOrElse[H >: G](default: ⇒ H): H

    Returns, if this Or is Good, this Good's value; otherwise returns the result of evaluating default.

    Returns, if this Or is Good, this Good's value; otherwise returns the result of evaluating default.

    default

    the default expression to evaluate if this Or is a Bad

    returns

    the contained value, if this Or is a Good, else the result of evaluating the given default

    Definition Classes
    BadOr
  23. val isBad: Boolean

    Indicates whether this Or is a Bad

    Indicates whether this Or is a Bad

    returns

    true if this Or is a Bad, false if it is a Good.

    Definition Classes
    BadOr
  24. val isGood: Boolean

    Indicates whether this Or is a Good

    Indicates whether this Or is a Good

    returns

    true if this Or is a Good, false if it is a Bad.

    Definition Classes
    Or
  25. final def isInstanceOf[T0]: Boolean

    Definition Classes
    Any
  26. def map[H](f: (G) ⇒ H): Or[H, B]

    Maps the given function to this Or's value if it is a Good or returns this if it is a Bad.

    Maps the given function to this Or's value if it is a Good or returns this if it is a Bad.

    f

    the function to apply

    returns

    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

    Definition Classes
    BadOr
  27. final def ne(arg0: AnyRef): Boolean

    Definition Classes
    AnyRef
  28. final def notify(): Unit

    Definition Classes
    AnyRef
  29. final def notifyAll(): Unit

    Definition Classes
    AnyRef
  30. def orElse[H >: G, C >: B](alternative: ⇒ Or[H, C]): Or[H, C]

    Returns this Or if it is a Good, otherwise returns the result of evaluating the passed alternative.

    Returns this Or if it is a Good, otherwise returns the result of evaluating the passed alternative.

    alternative

    the alternative by-name to evaluate if this Or is a Bad

    returns

    this Or, if it is a Good, else the result of evaluating alternative

    Definition Classes
    BadOr
  31. def swap: Or[B, G]

    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.scalautils.Bad[Nothing,String] =
        Bad(Hey Jude, don't make it bad. Take a sad song and make it better.)

    scala> lyrics.swap res12: org.scalautils.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 a Good, its Good value wrapped in a Bad; if this Or is a Bad, its Bad value wrapped in a Good.

    Definition Classes
    BadOr
  32. final def synchronized[T0](arg0: ⇒ T0): T0

    Definition Classes
    AnyRef
  33. def toEither: Either[B, G]

    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.

    returns

    this Good value, wrapped in a Right, or this Bad value, wrapped in a Left.

    Definition Classes
    BadOr
  34. def toOption: None.type

    Returns a Some containing the Good value, if this Or is a Good, else None.

    Returns a Some containing the Good value, if this Or is a Good, else None.

    returns

    the contained “good” value wrapped in a Some, if this Or is a Good; None if this Or is a Bad.

    Definition Classes
    BadOr
  35. def toSeq: IndexedSeq[G]

    Returns an immutable IndexedSeq containing the Good value, if this Or is a Good, else an empty immutable IndexedSeq.

    Returns an immutable IndexedSeq containing the Good value, if this Or is a Good, else an empty immutable IndexedSeq.

    returns

    the contained “good” value in a lone-element Seq if this Or is a Good; an empty Seq if this Or is a Bad.

    Definition Classes
    BadOr
  36. def toTry(implicit ev: <:<[B, Throwable]): Failure[G]

    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.

    returns

    this Good value, wrapped in a Right, or this Bad value, wrapped in a Left.

    Definition Classes
    BadOr
  37. def transform[H, C](gf: (G) ⇒ Or[H, C], bf: (B) ⇒ Or[H, C]): Or[H, C]

    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.

    gf

    the function to apply to this Or's Good value, if it is a Good

    bf

    the function to apply to this Or's Bad value, if it is a Bad

    returns

    the result of applying the appropriate one of the two passed functions, gf or bf, to this Or's value

    Definition Classes
    BadOr
  38. final def wait(): Unit

    Definition Classes
    AnyRef
    Annotations
    @throws()
  39. final def wait(arg0: Long, arg1: Int): Unit

    Definition Classes
    AnyRef
    Annotations
    @throws()
  40. final def wait(arg0: Long): Unit

    Definition Classes
    AnyRef
    Annotations
    @throws()
  41. def withFilter[C >: B](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

Inherited from Serializable

Inherited from Serializable

Inherited from Product

Inherited from Equals

Inherited from Or[G, B]

Inherited from AnyRef

Inherited from Any

Ungrouped