Type inference in Dart

Type inference in Dart

implicit datatype inference and dynamic variables in Dart.

ยท

3 min read

Now that we've learnt about the primitive data types in Dart, let us look at how type inference works in Dart.

Type inference is a language's ability to infer type when the user has not specified. In the previous tutorial, we were following this syntax to declare variables.

image.png

However we can also use the following syntax to declare a variable. We can decalre a variable without explicitly declaring the data type by using the keyword var in place of dataType.

nd Dart is strongly typed language. What that means is Dart will use a combination of static type checks and runtime checks to ensure that a variable's value always matches it's static type.

image.png

main() {
  var bookTitle = "Harry Potter";
  var numPages = 500;

  // Printing the value
  print(bookTitle);
  print(numPages);

  // printing the type
  print(bookTitle.runtimeType);
  print(numPages.runtimeType);
}

# Output:
Harry Potter
500
String
int

In the above example, we can see that we are creating two variables with string and number values by using the keyword var instead of explicitly mentioning the data-type.

As we've already learnt that everything in Dart is an object, including all the variables. These variable objects have a property called runtimeType which holds information about the data-type of the variable.

Now that we have seen Dart can infer the type of data, let us see what happens when we re-assign value to the inferred variables.

main() {
  var number = 5;
  number = 5.67;
}

Output:
main.dart:3:12: Error: A value of type 'double' can't be assigned to a variable of type 'int'.
Try changing the type of the left hand side, or casting the right hand side to 'int'.
  number = 5.67;

As we can see, when we assigned 5 to number, it was inferred as type int hence later we are not able to assign a decimal value to this variable later.

One of the ways to handle the above error would be to declare number variable as num number so that it can accept both int and double values.

main() {
  num number = 5;
  print(number.runtimeType);
  number = 5.67;
  print(number.runtimeType);
}

Output:
int
double
main() {
  dynamic number = 5;
  print(number.runtimeType);
  number = 5.67;
  print(number.runtimeType);
}

# output
int
double

Dynamic type

However what happens if we want to use the same variable to store different data types like number, string, boolean etc? If we want a variable to be able to store objects of different datatypes, we have to declare the variable using dynamic keyword.

main() {
  dynamic var1 = 'A string'; // type String
  print(var1.runtimeType);

  var1 = 5; // type int
  print(var1.runtimeType);

  var1 = true; // type bool
  print(var1.runtimeType);
}

output:
String
int
bool

Here we can see that on declaring num1 as dynamic type, it is able to store string, number or boolean values without any issues.

That's it for now

When writing program, now you know how to declare your variables correctly based on your programming needs.

Happy coding. Cheers !๐Ÿป

ย