dart-decimal icon indicating copy to clipboard operation
dart-decimal copied to clipboard

Add `Decimal` prefix to extensions

Open AlexV525 opened this issue 9 months ago • 8 comments

When I was trying to import package:decimal and package:rational simultaneously, the analyzer complained that they had a conflicting set of extensions.

The name 'BigIntExt' is defined in the libraries 'package:decimal/decimal.dart' and 'package:rational/rational.dart'.

P.S. I would not expect the change to be a breaking change.

AlexV525 avatar Mar 03 '25 12:03 AlexV525

I just try the following code and see no error from analyzer with Dart-3.8.1:

import 'package:decimal/decimal.dart';
import 'package:rational/rational.dart';

void main() {
  print(1.toDecimal());
  print(1.toRational());
  print(BigInt.one.toDecimal());
  print(BigInt.one.toRational());
}

What's your Dart version?

a14n avatar Jun 15 '25 14:06 a14n

It could be 3.6.x IIRC.

AlexV525 avatar Jun 15 '25 15:06 AlexV525

Okay it was about exporting them simultaneously, not importing. Sorry for the confusion.

export 'package:decimal/decimal.dart' show BigIntExt;
export 'package:rational/rational.dart' show BigIntExt;

This will raise:

The name 'BigIntExt' is defined in the libraries 'package:decimal/decimal.dart' and 'package:rational/rational.dart'.

Try removing the export of one of the libraries, or explicitly hiding the name in one of the export directives.

AlexV525 avatar Jun 16 '25 01:06 AlexV525

TBH I'm not a big fan of this change. This kind of export of the same name is really uncommon IMHO. OTOH I can't find a workaroud to make it work without renaming. It could be worth it asking on dart-lang/language. Out of curriosity why are you exporting whose one together? I expected to use one or the other, not both.

a14n avatar Jun 19 '25 20:06 a14n

This kind of export of the same name is really uncommon IMHO. Out of curriosity why are you exporting whose one together? I expected to use one or the other, not both.

The example was the minimum, the actual case is I want to export both packages for using:

export 'package:decimal/decimal.dart';
export 'package:rational/rational.dart';

Which will also raise the lint.

In some cases, I also needed Rational, such as / operator and pow method. Those classes didn't get exported from package:decimal so I need to import package:rational as an extra effort. Then I came up with the idea to export them both from my general export point.

AlexV525 avatar Jun 21 '25 06:06 AlexV525

I think the issue is a language issue (but I can't find a relevant issue number on https://github.com/dart-lang/language). eg. a language solution could be to allow typedef on extension:


export 'package:decimal/decimal.dart' hide IntExt, BigIntExt;
export 'package:rational/rational.dart' hide IntExt, BigIntExt;
import 'package:decimal/decimal.dart' as decimal;
import 'package:rational/rational.dart' as  rational;

typedef DecimalBigIntExt = decimal.BigIntExt;
typedef DecimalIntExt = decimal.IntExt;
typedef RationalBigIntExt = rational.BigIntExt;
typedef RationalIntExt = rational.IntExt;

A workaround can be to duplicate the code :

export 'package:decimal/decimal.dart' hide IntExt, BigIntExt;
export 'package:rational/rational.dart' hide IntExt, BigIntExt;
import 'package:decimal/decimal.dart';
import 'package:rational/rational.dart';

/// Extensions on [BigInt].
extension BigIntExt on BigInt {
  /// This [BigInt] as a [Rational].
  Rational toRational() => Rational(this);

  /// This [BigInt] as a [Decimal].
  Decimal toDecimal() => Decimal.fromBigInt(this);
}

/// Extensions on [int].
extension IntExt on int {
  /// This [int] as a [Rational].
  Rational toRational() => Rational.fromInt(this);

  /// This [int] as a [Decimal].
  Decimal toDecimal() => Decimal.fromInt(this);
}

a14n avatar Jun 22 '25 21:06 a14n

I think the issue is a language issue.

Indeed, I shall file an issue referencing this.

Should we continue or wait until a fix for the language has been implemented?

AlexV525 avatar Jun 23 '25 03:06 AlexV525

If you can live with the above workaround I'll prefer to wait for an answer of the language issue you will file

a14n avatar Jun 23 '25 07:06 a14n