Scalar.typed constructor

Scalar.typed(
  1. DType dtype,
  2. Object? value
)

Implementation

factory Scalar.typed(DType dtype, Object? value) {
  if (value == null) return Scalar.nullValue(dtype);
  if (dtype.kind case final kind when _integerBounds.containsKey(kind)) {
    final integer = switch (value) {
      BigInt value => value,
      int value => BigInt.from(value),
      String value => BigInt.tryParse(value),
      _ => null,
    };
    if (integer == null) {
      throw ArgumentError.value(value, 'value', 'must be an integer');
    }
    return Scalar.integer(integer, dtype);
  }
  return switch (dtype) {
    BooleanType() when value is bool => Scalar.boolean(value),
    Float16Type() when value is int => Scalar.float16Bits(value),
    Float32Type() when value is double => Scalar.float32(value),
    Float64Type() when value is double => Scalar.float64(value),
    StringType() when value is String => Scalar.string(value),
    BinaryType() when value is List<int> => Scalar.binary(value),
    BinaryOffsetType() when value is List<int> => Scalar.binary(
      value,
      offset: true,
    ),
    DateType() when value is int => Scalar.date(value),
    TimeType() when value is int => Scalar.time(value),
    DateTimeType() => Scalar.datetime(_asBigInt(value), dtype),
    DurationType() => Scalar.duration(_asBigInt(value), dtype),
    DecimalType() => Scalar.decimal(_asBigInt(value), dtype),
    _ => throw ArgumentError.value(
      value,
      'value',
      'does not match ${dtype.kind}',
    ),
  };
}