Introduction

Those who are familiar with JAVA may know that JAVA introduced the concept of generics in 8. What is generic? Generic is a general type format, which is generally used in collections to specify the format of objects that should be stored in the collection.

With generics, our programming can be simplified, and errors can be reduced, which is very convenient.

There are also generics in the dart language. Let's take a look.

Why use generics

The main purpose of using generics is to ensure type safety. For example, if we have a List, and we only want to store the String type in the List, then we can specify this in dart:

var stringList = <String>[];
stringList.addAll(['jack ma', 'tony ma']);
stringList.add(18); // 报错

Then in the process of using, you can only add strings to stringList. If you add numbers to it, an error will be reported, thus ensuring the consistency of the types in the List.

The clever use of generics can also reduce the amount of our code, because generics can represent a common type.

For example, in a school, we have a dormitory, and the dormitory is divided into men and women, so there is this definition for boys:

abstract class BoyRoom {
    Boy getByName(String name);
}

For girls, there is this definition:

abstract class GirlRoom{
    Girl getByname(String name);
}

In fact, there is not much difference between the two in essence, but the type of the parameter or return value has changed, then we can write like this:

abstract class Room<T>{
    T getByname(String name);
}

This simplifies the use of code.

How to use generics

Generics are generally represented by a single uppercase character, usually E, T, S, K, and V.

The most common use of generics is in collections, such as List, set and map:

var listExample = <String>['jack ma', 'tony ma'];
var setExamples = <String>{'jack ma', 'tony ma'};
var mapExamples = <String, String>{
  'name1': 'jack ma',
  'name2': 'tony ma',
};

Generics can also be used in the constructors of these collection classes, as follows:

var stringMap = Map<String, String>();

Indicates that the constructed set should contain the corresponding type.

Type erasure

Although there are generics in JAVA, generics in JAVA have a feature of type erasure. When is type erasure? Type erasure refers to the type specified by generics, which only takes effect at compile time, and there is no concept of generics at runtime.

For a List<String>, JAVA can only judge whether the object is a List at runtime, but cannot judge whether the object is a List<String>.

Dart is different from java. Dart can carry type information at runtime, that is to say, it can be judged whether an object is List<String> in dart.

var stringList = <String>[];
stringList.addAll(['jack ma', 'tony ma']);
print(names is List<String>); // true

Generic inheritance

The purpose of using generics is to limit the types of parameters, so we usually specify the parent class of generics to limit the scope of generic types:

class Room<T extends Student> {

}

class Boy extends Student {...}

In the process of using, you can pass in Student itself, or pass in Student's subclass Boy, or not pass:

var student = Room<Student>();
var boy = Room<Boy>();
var studentDefault = Room();

Generic method

In addition to being used in classes, generics in dart can also be used in methods:

T doSomething<T>(List<T> list) {
  T result = list[0];
  return result;
}

The generic type specified in the method can be used in the return type, parameter, and local variable type in the method.

Summarize

The above is an introduction to generics and their use in dart.

This article has been included in http://www.flydean.com/08-dart-generics/

The most popular interpretation, the most profound dry goods, the most concise tutorial, and many tips you don't know are waiting for you to discover!

Welcome to pay attention to my official account: "Program those things", know technology, know you better!


flydean
890 声望433 粉丝

欢迎访问我的个人网站:www.flydean.com