18

I am new to Dart and I can see Dart has num which is the superclass of int and double (and only those two, since it's a compile time error to subclass num to anything else).

So far I can't see any benefits of using num instead of int or double, are there any cases where num is better or even recommended? Or is it just to avoid thinking about the type of the number so the compiler will decide if the number is an int or a double for us?

Michel Feinstein
  • 13,416
  • 16
  • 91
  • 173

4 Answers4

22

One benefit for example before Dart 2.1 :

suppose you need to define a double var like,

double x ;

if you define your x to be a double, when you assign it to its value, you have to specify it say for example 9.876.

x = 9.876;

so far so good.

Now you need to assign it a value like say 9

you can't code it like this

x = 9;  //this will be error before dart 2.1

so you need to code it like

x = 9.0;

but if you define x as num

you can use

x = 9.0;

and

x = 9;

so it is a convenient way to avoid these type mismatch errors between integer and double types in dart.

both will be valid.

this was a situation before Dart 2.1 but still can help explain the concept

check this may be related

Saed Nabil
  • 6,705
  • 1
  • 14
  • 36
  • 1
    So no benefits after Dart 2.1? – Michel Feinstein Nov 27 '18 at 21:34
  • I don't think so, But I can not find practical use situation in my mind for now :) – Saed Nabil Nov 27 '18 at 21:40
  • 2
    In your example, `9` will be interpreted as the double `9.0`, but I'm not sure if `num` does that too. One situation where `num` can be used is for Javascript interop, since JS doesn't differentiate. Considering Dart was originally designed as a replacement for JS (but gave up on that idea a few years ago), it may have been included just for that purpose. – Jacob Phillips Nov 27 '18 at 23:37
  • @JacobPhillips num will interpret 9 as int 9 that is the purpose of num and that is why its mix type. and I think you are right about Dart and JS. – Saed Nabil Nov 28 '18 at 00:05
2

Not sure if this is useful to anyone, but I just ran into a case where I needed num in a way.

I defined a utility function like this:

T maximumByOrNull<T, K extends Comparable<K>>(Iterable<T> it, K key(T),) {
  return it.isEmpty
      ? null
      : it.reduce((a, b) => key(a).compareTo(key(b)) > 0 ? a : b);
}

And invoking it like this…

eldest = maximumByOrNull(students, (s) => s.age);

… caused trouble when age is an int, because int itself does not implement Comparable<int>.

So Dart cannot infer the type K in the invocation to maximumByOrNull.

However, num does implement Comparable<num>. And if I specified:

eldest = maximumByOrNull(students, (s) => s.age as num);         // this
eldest = maximumByOrNull<Student, num>(students, (s) => s.age);  // or this

the complaint went away.


Bottom line: it seems num implements Comparable when int and double do not, themselves, and sometimes this causes trouble for generic functions.

Lynn
  • 10,425
  • 43
  • 75
0

A good use case of num are extensions that work with int and double.

As an example I include the extension MinMax on List<num> that provides the getters min and max.

extension MinMax on List<num>{
  /// Returns the maximum value or `null` if the list is empty.
  num get max {
    return (isNotEmpty)
        ? fold<num>(0, (prev, current) => (prev > current) ? prev : current)
        : null;
  }

  /// Returns the minimum value or `null` if the list is empty.
  num get min {
    return (isNotEmpty)
        ? fold<num>(0, (prev, current) => (prev < current) ? prev : current)
        : null;
  }
}

Using the extension above one can access the min/max values without a need to create specific implementations for the classes int and double.

void main() {
  final a = <int>[1,3,5];
  final b = <double>[ 0.5, 0.8, -5.0];
  print(a.min);
  print(b.max);
  
}
Dan R
  • 93
  • 6
0

I just ran into a use case when it is useful. My app stores weight, which was originally defined as a double.

When using with a local database (sqlite) it works fine, since sqlite handles integer and real types.

However, when I converted my app to use Firestore database, I ran into issues with all my double fields. If a decimal value is stored everything works fine. However, when the weight happens to be a whole number, Firestore returns it as an int and suddenly type errors - int is not a subtype of type double - start to appear.

In the above scenario changing the double fields and variables to num was a quite simple solution.