ChoiceConfigKnob<T> constructor

ChoiceConfigKnob<T>(
  1. List<T> choices, {
  2. required T value,
  3. Map<T, String> choiceLabels = const {},
})

Creates a new knob to with the specified default value of the available choices.

If any member of choices is a Dart Type, choiceLabels must contain a stable label for every such member. These labels are used as both user-facing text and serialized JSON values:

ChoiceConfigKnob<Type>(
  [BinaryToOneHot, OneHotToBinary],
  value: OneHotToBinary,
  choiceLabels: const {
    BinaryToOneHot: 'BinaryToOneHot',
    OneHotToBinary: 'OneHotToBinary',
  },
);

Matching labels to the previous unminified Type.toString() values preserves compatibility with existing saved configurations. Every choice must resolve to a unique serialized value; duplicate labels and labels that collide with an unlabeled choice's string representation are rejected.

Implementation

ChoiceConfigKnob(
  this.choices, {
  required super.value,
  Map<T, String> choiceLabels = const {},
}) : choiceLabels = Map<T, String>.unmodifiable(choiceLabels) {
  if (!choices.contains(value)) {
    throw RohdHclException('Default value should be one of the choices.');
  }
  if (!this.choiceLabels.keys.every(choices.contains)) {
    throw RohdHclException('Choice labels should correspond to choices.');
  }
  if (choices
      .whereType<Type>()
      .any((choice) => !this.choiceLabels.containsKey(choice))) {
    throw RohdHclException(
        'Type choices require stable labels for minified builds.');
  }
  final serializedValues = choices.map(_serializedValue).toList();
  if (serializedValues.toSet().length != serializedValues.length) {
    throw RohdHclException(
        'Choices must have unique serialized values and labels.');
  }
}