Learn about the Dart language – Part VIII

Tram Ho

Generics

If you look at the API documentation for the basic array type – List, you’ll see that that type is defined List <E>. The symbols <…> indicate that List is a generic (or parameterized) type. By convention, most type variables have a single name, such as E, T, S, K and V.

Why use generic types?

Generics are often required for types that require security, but they have more benefits than just allowing your code to run:

  • Properly specifying generic types leads to better generated code.
  • You can use generic to reduce code duplication.

If you intend for a List to contain only Strings, you can declare it as List <String> (read that List as a List of String). That way, you, your fellow programmers, and tools can discover that assigning a non-String object to the List can be a mistake. Here is an example:

Another reason to use generics is to reduce code duplication. Generics allow you to share a single interface and deploy between multiple types, while still leveraging static analysis. For example, suppose you create an interface to store objects:

You discover that you want a version specific to this interface, so you create another interface:

Then you decide you want a specific digital version of this interface. So you have an idea.

Generic styles can save you the hassle of creating all these interfaces. Instead, you can create a unique, generic interface:

In this code, T is independent. It is a place holder that you can think of as a type that developers will identify later.

Use collection literals

List, Set and Map can be parameterized. The parameterization is the same as you saw above, except that you add (for List and Set) <type> or (for Map) <keyType, valueType> before the opening parenthesis. Here is an example of using typed letters:

Use common types with constructors

To specify one or more types when using the constructor, enclose the styles in curly braces (<…>) immediately after the class name. For example:

The following code creates the Map with the integer keys and values ​​of the View type:

General collections and the types they contain

In Dart, generic types are unified, meaning they carry information about their runtime type. For example, you can check the type of Collection:

Note: In contrast, generic in Java uses erasure , which means that generic type parameters are removed at runtime. In Java, you can check if an object is a List, but you cannot check if it is a List <String>.

Restrict common type

When implementing a generic type, you may want to limit its parameter types. You can do this by using extends .

You can use SomeBaseClass or any of its subclasses as generic arguments:

You can also specify no common arguments:

Specifying any type that is not a child of SomeBaseClass will cause an error:

Use the common method

At first, Dart’s general support was limited to classes. A newer syntax, called generic methods, allows common type arguments on methods and functions:

Here, the generic type parameter on first (<T>) allows you to use the type T argument in some places:

  • In the return type of the function (T).
  • In the type of an argument (List <T>).
  • In local variable type (T tmp). For more information about generic, see Using the Generic Method .

Source https://dart.dev/guides/language/language-tour#generics

Share the news now

Source : Viblo