Table of contents
1.
Introduction
2.
Dart Type System
2.1.
Example
3.
Soundness
4.
Frequently Asked Questions
4.1.
How does soundness increase the readability of the code?
4.2.
How does soundness help in maintaining the code?
4.3.
How does a sound type system help in catching type-related errors at compile time?
5.
Conclusion 
Last Updated: Mar 27, 2024

Dart Type System

Author Pradeep Kumar
1 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In this blog, we will look into the Dart Type System. When debugging code at compile time, the Dart Type System is quite useful. If the static and dynamic type checking of a particular variable produces different data types, it will throw an error, which is quite helpful while debugging the program. If you are new to Dart and are yet to set up your environment for the language, you can check out this article.

Dart Type System

Dart is a safety-enabled language that combines static and dynamic type checking at compile time to match the value of a variable to its data type. Sound typing is another name for this. Because of the type interference, data type annotations are optional, even though the types are required.

Adding type annotations to generic classes can fix all kinds of static problems. The following are some of the most commonly used generic collection classes:

  • List
  • Map

Example

The below code will throw an error while printing the list:

void printList(List<int> l) {
  // Printing the list
  print(l);
}

// Main function to run the program
void main() {
  // Initialising a list
  List l = [];
  // Adding an integer value to the list
  l.add(1);
  // Adding a string value to the list
  l.add("Red");
  // Adding a float value to the list
  l.add(3.4);

  // Calling the print function to print the list
  printList(l);
}

Output:

The argument type 'List<dynamic>' can't be assigned to the parameter type 'List<int>'

The above code throws this error because the function expects a list of integer datatype. But the list passed to the function contains elements of different data types. 

One of the methods to remove the above error is to remove the int data type from the list in the printList function. For example,

void printList(List l) {
  // Printing the list
  print(l);
}

// Main function to run the program
void main() {
  // Initialising a list
  List l = [];
  // Adding an integer value to the list
  l.add(1);
  // Adding a string value to the list
  l.add("Red");
  // Adding a float value to the list
  l.add(3.4);

  // Calling the print function to print the list
  printList(l);
}

Output:

[1, Red, 3.4]

Soundness

Soundness aids in the program's effective passage through invalid states. A sound type system ensures that the program never reaches a point when an expression is evaluated to a value that does not match the expression's static type. When you evaluate an expression with the type 'String' at compile-time, you are assured of getting only a string when you evaluate it at run time.

Advantages of having soundness:

  • Increases Code Readability
  • Increase code maintainability
  • Catching type-related bugs at compile time
  • Ahead Of Time (AOT) compilation is one of its core characteristics, which drastically reduces the code's compile time and improves efficiency.

Frequently Asked Questions

How does soundness increase the readability of the code?

With sound type safety, code becomes more understandable since one can trust that a value has the exact data type declared. Types can't lie in sound Dart.

 

How does soundness help in maintaining the code?

When a piece of code, such as the data type of a variable, is changed, the type system highlights and notifies the programmer about the other locations where the previous data type is still in use.

 

How does a sound type system help in catching type-related errors at compile time?

A good type system ensures that the code is unambiguous in terms of data types, making it easier to identify type-related issues at compile time.

Conclusion 

In this article, we have extensively discussed the Type System in Dart. We hope that this blog has helped you enhance your knowledge regarding the Dart Type System, and to learn more, check out our other article on Dart Operators. And also check out the awesome content on the Coding Ninjas Website,

To study more about data types, refer to Abstract Data Types in C++.
Android DevelopmentCoding Ninjas Studio ProblemsCoding Ninjas Studio Interview BundleCoding Ninjas Studio Interview ExperiencesCoding Ninjas CoursesCoding Ninjas Studio Contests, and Coding Ninjas Studio Test Series. Do upvote our blog to help other ninjas grow. Happy Coding!

Live masterclass