Bounded

Bounded is a type class that defines the minimum and maximum values for a specific type. It provides methods for obtaining these bounds, enabling work with types that have a limited range of values.

Bounded is used in various scenarios where knowing the bounds of a type's values is important. Here are some examples:

Formal definition

Bounded is a type class extending Order that defines the minimum and maximum values for a specific type. Bounded provides methods for obtaining these bounds (min and max), enabling work with types that have a limited range of values.

Laws

For a correct implementation of Bounded, the following laws must hold:

Code definition in Scala

trait Equal[F]:
  def equal(a1: F, a2: F): Boolean

enum Ordering:
  case LT
  case EQ
  case GT

trait Order[F] extends Equal[F]:
  def order(x: F, y: F): Ordering

trait Bounded[F] extends Order[F]:
  def min: F
  def max: F
  1. Type parameter F: Bounded is parameterized by type F for which bounds are defined.
  2. min method: Returns the minimum value of type F.
  3. max method: Returns the maximum value of type F.

Diagram

classDiagram
    class Equal~A~{
        +equal(x: A, y: A) Boolean
    }
    class Ordering{
        <<enumeration>>
        LT
        EQ
		GT
    }
    class Order~A~{
        +order(x: A, y: A) Ordering
    }
    Ordering .. Order
    Equal <|-- Order
    class Bounded~A~{
        +min: A
        +max: A
    }
    Order <|-- Bounded

References: