ofInferWidth static method

LogicValue ofInferWidth(
  1. dynamic val
)

Creates a LogicValue of val using of, but attempts to infer the width that would fit val automatically.

Only accepts vals of types int, BigInt, and LogicValue.

The width of negative numbers cannot be inferred and an exception will be thrown.

Implementation

static LogicValue ofInferWidth(dynamic val) {
  int width;
  if (val is int) {
    if (val < 0) {
      throw LogicValueConstructionException(
          'Cannot infer width of a negative int.');
    } else {
      width = val.bitLength;
    }
  } else if (val is BigInt) {
    if (val.isNegative) {
      throw LogicValueConstructionException(
          'Cannot infer width of a negative BigInt.');
    } else {
      width = val.bitLength;
    }
  } else if (val is LogicValue) {
    width = val.width;
  } else {
    throw UnsupportedTypeException(val, const [int, BigInt, LogicValue]);
  }

  return LogicValue.of(val, width: width);
}