TS data type: From C++/Java/Python to TS to look at tuples—the ins and outs of tuples

In a strongly typed language such as C++ and Java, the commonly used Array, List, and Set can only be one type in the collection (you can review: " Talking about Java Data Structure-Analysis of the underlying implementation and application considerations ).

int data[] = new int[3]
List<String> person=new ArrayList<>();
Set<Integer> test = new TreeSet<>();

If we need a fixed size collection of different types of values, we need to use tuples

Introduction to C++ Tuples

Tuple is a powerful container that allows storing multiple different types of data, and is a generalization of pair. It can also be used as a general structure, without the need to create a structure and obtain the characteristics of the structure. In some cases, it can replace the structure to make the program more concise and intuitive. In theory, std::tuple can have an infinite number of member variables of any type, and std::pair can only be 2 members, so when you need to save 3 or more data, you need to use tuple tuples (tuple (element Group) began to be quoted in c++11.).

std::tuple<T1, T2, TN> t1; //Create an empty tuple object (using the default construction), the corresponding elements are T1 and T2...Tn types, using value initialization
std::tuple<T1, T2, TN> t2(v1, v2, ... TN); //Create a tuple object, its two elements are T1 and T2 ... Tn type; to get the element The value needs to be obtained through the member get<Ith>(obj) of the tuple (Ith refers to the number of elements in the tuple, please see the following specific examples).
std::tuple<T1&> t3(ref&); // The element type of tuple can be a reference
std::make_tuple(v1, v2); // Like a pair, you can also create a tuple object through make_tuple

The objects in the tuple can be of any different type and have any length

Java Tuple

Tuple, which is a single object in which a set of objects are directly packaged and stored. This container object allows the elements in it to be read. But it is not allowed to store new objects in it. (This concept is also called data transfer Object, or messenger)

python tuple

Python's collective data type list and tuple are very similar, the difference is:

Tuple is defined by parentheses (), and the element content cannot be edited after definition (ie immutable), while list is defined by square brackets [], and its element content can be edited (that is, changeable). Editing actions include deleting pop() and appending to the end append( ), insert insert( ).

list1=['cong','rick','long']//list
tupe1=('Jan','Feb','Mar')//tuple, tuple elements cannot be modified, cannot be added or deleted.
tupeformList = tuple(list1)

The nature of tuples in python is similar to java

Tuples, like lists, may be used for data storage and contain multiple data;

But the difference with the list is that the list can only store the same data type, but the tuple is different. It can store different data types, such as storing int, string, list, etc. at the same time, and can be expanded indefinitely according to needs.

TypeScript Tuple

TypeScript injects strong typing into JavaScript, making JavaScript more like Java, such as:

let sites:string[]  ----> String args[]

JavaScript数组本来就可以存放任意类型。但是TypeScript,就变成元组了。其实你不了解元组这个概念也可以照样用。就当类型都要指定就行。

let myTuple: [number, string, boolean] = [1, 'Poplar', true];

对于越界的元素,它的类型会被限制为元组中每个类型的联合类型

TypeScript元组继承自于函数编程语言(如 F#)——一家亲。

参考文章:

C++ tuple元组的基本用法(总结)  https://blog.csdn.net/sevenjoin/java/article/details/88420885

C++中使用tuple https://blog.csdn.net/xiahn1a/article/details/41670203

Java元组Tuple介绍与使用 https://blog.csdn.net/u013412772/article/details/74298257



转载本站文章《TS数据类型:从C++/Java/Python到TS看元组tuple—元组的来龙去脉》,
请注明出处:https://www.zhoulujun.cn/html/webfront/ECMAScript/typescript/2020_0413_8380.html


Guess you like

Origin blog.51cto.com/zhoulujun/2535581