byIndex<T extends Enum> static method

T Function(dynamic) byIndex<T extends Enum>(
  1. List<T> values
)

Creates a parser that matches enum values by their numeric index.

The input is converted to int using ConvertObjectImpl.toInt, which supports strings like "1" and numeric types.

Throws ArgumentError if the index is out of bounds (must be 0 to values.length - 1).

Implementation

static T Function(dynamic) byIndex<T extends Enum>(List<T> values) =>
    (dynamic obj) {
      final index = ConvertObjectImpl.toInt(obj);
      if (index < 0 || index >= values.length) {
        throw ArgumentError(
          'Invalid enum index: $obj (valid range: 0-${values.length - 1})',
        );
      }
      return values[index];
    };