operator [] method

int operator [](
  1. int index
)

Allows retrieving numerator or denominator by index. In particular, ´0´ refers to the numerator and ´1´ to the denominator.

Throws a FractionException object if index is not 0 or 1.

Implementation

int operator [](int index) {
  if (index == 0) {
    return numerator;
  }

  if (index == 1) {
    return denominator;
  }

  throw FractionException(
    'The index you gave ($index) is not valid: it must be either 0 or 1.',
  );
}