whenOrElse<R> method

R whenOrElse<R>(
  1. Map<Enum, R Function()> map, {
  2. required R orElse(),
})

A when or else function for Enums. It takes a Map of Enums to functions, and returns the result of calling the function corresponding to the Enum of the object.

Implementation

R whenOrElse<R>(Map<Enum, R Function()> map, {required R Function() orElse}) {
  if (map.containsKey(this)) {
    return map[this]!();
  } else {
    return orElse();
  }
}