dartx icon indicating copy to clipboard operation
dartx copied to clipboard

Add 'num.minus()' and `num.plus()` for null-safety

Open yongjhih opened this issue 5 years ago • 2 comments

I'm not sure if it's a good idea for this project. That would be better if there is a nullable +operator we can overwrite such as 1 +? nullable similar to 1 as? String

  test('should num?.minus()', () async {
    final num value = null;
    expect(value?.minus(1), null);
    expect(value?.minus(1) ?? -1, -1);
    expect((value ?? 0) - 1, -1);
    expect(() => value - 1, throwsNoSuchMethodError);

    expect(value?.plus(1), null);
    expect(value?.plus(1) ?? 1, 1);
    expect((value ?? 0) + 1, 1);
    expect(() => value + 1, throwsNoSuchMethodError);

    expect(1.plus(value), 1);
    expect(1.plusOrNull(value), null);
    expect(1.plusOrNull(value) ?? 1, 1);
    expect(() => 1 + value, throwsNoSuchMethodError);

    expect(1.minus(value), 1);
    expect(1.minusOrNull(value), null);
    expect(1.minusOrNull(value) ?? 1, 1);
    expect(() => 1 - value, throwsNoSuchMethodError);
  });
  test('should int?.minus()', () async {
    final int value = null;
    expect(value?.minus(1), null);
    expect(value?.minus(1) ?? -1, -1);
    expect((value ?? 0) - 1, -1);
    expect(() => value - 1, throwsNoSuchMethodError);

    expect(value?.plus(1), null);
    expect(value?.plus(1) ?? 1, 1);
    expect((value ?? 0) + 1, 1);
    expect(() => value + 1, throwsNoSuchMethodError);

    expect(1.plus(value), 1);
    expect(1.plusOrNull(value), null);
    expect(1.plusOrNull(value) ?? 1, 1);
    expect(() => 1 + value, throwsNoSuchMethodError);

    expect(1.minus(value), 1);
    expect(1.minusOrNull(value), null);
    expect(1.minusOrNull(value) ?? 1, 1);
    expect(() => 1 - value, throwsNoSuchMethodError);
  });
  test('should double?.minus()', () async {
    final double value = null;
    expect(value?.minus(1), null);
    expect(value?.minus(1) ?? -1, -1);
    expect((value ?? 0) - 1, -1);
    expect(() => value - 1, throwsNoSuchMethodError);

    expect(value?.plus(1), null);
    expect(value?.plus(1) ?? 1, 1);
    expect((value ?? 0) + 1, 1);
    expect(() => value + 1, throwsNoSuchMethodError);

    expect(1.0.plus(value), 1);
    expect(1.0.plusOrNull(value), null);
    expect(1.0.plusOrNull(value) ?? 1, 1);
    expect(() => 1.0 + value, throwsNoSuchMethodError);

    expect(1.0.minus(value), 1);
    expect(1.0.minusOrNull(value), null);
    expect(1.0.minusOrNull(value) ?? 1, 1);
    expect(() => 1.0 - value, throwsNoSuchMethodError);
  });
extension IntArithmeticX<T extends int> on T {
  T minus(T it) => it != null ? this - it : this;
  T minusOrNull(T it) => it != null ? this - it : null;
  T plus(T it) => it != null ? this + it : this;
  T plusOrNull(T it) => it != null ? this + it : null;
}

extension NumArithmeticX<T extends num> on T {
  T minus(T it) => it != null ? this - it : this;
  T minusOrNull(T it) => it != null ? this - it : null;
  T plus(T it) => it != null ? this + it : this;
  T plusOrNull(T it) => it != null ? this + it : null;
}

extension DoubleArithmeticX<T extends double> on T {
  T minus(T it) => it != null ? this - it : this;
  T minusOrNull(T it) => it != null ? this - it : null;
  T plus(T it) => it != null ? this + it : this;
  T plusOrNull(T it) => it != null ? this + it : null;
}

yongjhih avatar Jan 13 '20 19:01 yongjhih

I can work on this one.

rishabhdeepsingh avatar Jun 06 '21 22:06 rishabhdeepsingh

I like it, please open a PR :)

passsy avatar Jun 08 '21 00:06 passsy

can someone close this issue? it has been merged a year ago

hugobrancowb avatar Oct 01 '22 01:10 hugobrancowb