2.Built-in types-基本数据类型(Dart中文文档)
- 初次翻译,部分内容并非按字面翻译,是按本人理解进行了内容重组。如有错误望指正。
Dart语言内置如下数据类型: - numbers
- strings
- booleans
- lists (所谓的数组)
- maps
- runes (for expressing Unicode characters in a string)
- symbols
你可以通过直接赋值指定变量为如上几个基本数据类型,比如,‘this is a string’就是String类型的值,true就是boolean类型的值。
Numbers
Dart numbers类型由两种:
int
整型,值最大不超过64bits,占用内存大小根据各平台VM的实现来定。在DartVM中,值区间为-2的63次方到2的63次-1,在javaScript中,数值区间为-2的53次方到2的53-1次方
double
64位双精度浮点型数据,是按照IEEE 754 标准定义。
int和double都是num型的子类,num型可以支持+, -, /, 和*,还有abs(),ceil(),floor的数学类操作。其中int类型可以支持 >>类似的移位操作。如果num型数据不符合你的要求,可以在dart:math 库中需要其它数值类型。
变量赋值没有小数点时,VM自动类型判断其为int
var x = 1;
var hex = 0xDEADBEEF;
如果变量包含小数点,VM自动类型判断其为double
var y = 1.1;
var exponents = 1.42e5;
从Dart2.1开始,int型在特定条件下自动转为double型,比如下方的写法,但是在Dart2.1之前,如下写法将报错
double z = 1; // Equivalent to double z = 1.0
如下是num和string互相转换的示例:
// String -> int
var one = int.parse('1');
assert(one == 1);
// String -> double
var onePointOne = double.parse('1.1');
assert(onePointOne == 1.1);
// int -> String
String oneAsString = 1.toString();
assert(oneAsString == '1');
// double -> String
String piAsString = 3.14159.toStringAsFixed(2);
assert(piAsString == '3.14');
int型数据支持位运算(<<,>>,& ,|)
assert((3 << 1) == 6); // 0011 << 1 == 0110
assert((3 >> 1) == 1); // 0011 >> 1 == 0001
assert((3 | 4) == 7); // 0011 | 0100 == 0111
数值常量支持二次计算赋值(我感觉要表述的是这个意思
Literal numbers are compile-time constants. Many arithmetic expressions are also compile-time constants, as long as their operands are compile-time constants that evaluate to numbers.)
const msPerSecond = 1000;
const secondsUntilRetry = 5;
const msUntilRetry = secondsUntilRetry * msPerSecond;
Strings
A Dart string is a sequence of UTF-16 code units. You can use either single or double quotes to create a string:
Dart的字符串采用的是UTF-16的编码,定义一个字符串可以采用单引号或者双引号
var s1 = 'Single quotes work well for string literals.';
var s2 = "Double quotes work just as well.";
var s3 = 'It\'s easy to escape the string delimiter.';
var s4 = "It's even easier to use the other delimiter.";
你可以在字符串中使用${expression}表达式作为动态内容,如果expression可以明确推断,可以去除{}.使用表达式时,表达式关联的对象将调用toString()方法
var s = 'string interpolation';
assert('Dart has $s, which is very handy.' ==
'Dart has string interpolation, ' +
'which is very handy.');
assert('That deserves all caps. ' +
'${s.toUpperCase()} is very handy!' ==
'That deserves all caps. ' +
'STRING INTERPOLATION is very handy!');
注意:两个字符串是否相同,取决是字符串的文本是否相同,并非要求其指向相同的内存地址
你可以通过+号拼接字符串。如果是需要换行形式的字符串,可以用三个单引号或者双引号来定义字符串
var s1 = 'String '
'concatenation'
" works even over line breaks.";
assert(s1 ==
'String concatenation works even over '
'line breaks.');
var s2 = 'The + operator ' + 'works, as well.';
assert(s2 == 'The + operator works, as well.');
var s1 = '''
You can create
multi-line strings like this one.
''';
var s2 = """This is also a
multi-line string.""";
你可以创建一个raw格式的字符串,正常字符串采用UTF-16字符编码,raw格式采用UTF-32字符编码。(这是本人理解,不知道对不对)
var s = r'In a raw string, not even \n gets special treatment.';
See Runes for details on how to express Unicode characters in a string.
如果字符串是const常量,其中的表达式也应该是字符串常量。
// These work in a const string.
const aConstNum = 0;
const aConstBool = true;
const aConstString = 'a constant string';
// These do NOT work in a const string.
var aNum = 0;
var aBool = true;
var aString = 'a string';
const aConstList = [1, 2, 3];
const validConstString = '$aConstNum $aConstBool $aConstString';
// const invalidConstString = '$aNum $aBool $aString $aConstList';
Booleans
boolean类型的值只有两种,分别是true和false,它们是内置常量。
Dart是类型安全的。这样意味者if (nonbooleanValue) ,assert (nonbooleanValue)中的值必须是boolean类型,不能是其它类型,否则编译不通过
// Check for an empty string.
var fullName = '';
assert(fullName.isEmpty);
// Check for zero.
var hitPoints = 0;
assert(hitPoints <= 0);
// Check for null.
var unicorn;
assert(unicorn == null);
// Check for NaN.
var iMeantToDoThis = 0 / 0;
assert(iMeantToDoThis.isNaN);
Lists
Dart的List创建写法JavaScript的数组,下面是一个简单例子:
var list = [1, 2, 3];
注意:如果数组定义是,限定了类型,比如限定为int,其添加元素都必须为int,否则会报错
。
数组的计数采用从0开始,那么数组第一个元素定位就写为list[0],数组最后一个元素定位就协议list[list.length-1]。具体的写法和JavaScript相似。
var list = [1, 2, 3];
assert(list.length == 3);
assert(list[1] == 2);
list[1] = 1;
assert(list[1] == 1);
如果数组赋值采用const修饰,数组元素不可修改。
var constantList = const [1, 2, 3];
// constantList[1] = 1; // Uncommenting this causes an error.
Maps
map类型由key和value组成,它们可以是任意类型对象,key值是不可重复的,value值可以重复。Dart支持定义一组key/value来创建map对象,类型json的写法。
var gifts = {
// Key: Value
'first': 'partridge',
'second': 'turtledoves',
'fifth': 'golden rings'
};
var nobleGases = {
2: 'helium',
10: 'neon',
18: 'argon',
};
注意:代码分析器会根据key/value值,判断map对象的类型,比如,gifts判定为Map<String,String>,nobleGases判定了Map<int,String>,如果添加其它类型的key/value可能导致编译出错或者运行出错。
你可以通过Map构造器创建Map对象,如下图所示:
var gifts = Map();
gifts['first'] = 'partridge';
gifts['second'] = 'turtledoves';
gifts['fifth'] = 'golden rings';
var nobleGases = Map();
nobleGases[2] = 'helium';
nobleGases[10] = 'neon';
nobleGases[18] = 'argon';
对已创建的map对象添加数据写法类似JavaScript的写法,具体如下:
var gifts = {'first': 'partridge'};
gifts['fourth'] = 'calling birds'; // Add a key-value pair
var gifts = {'first': 'partridge'};
assert(gifts['first'] == 'partridge');
如果key值在map中不存在,返回null对象
var gifts = {'first': 'partridge'};
assert(gifts['fifth'] == null);
你可以使用.length获取map对象的key数量。
var gifts = {'first': 'partridge'};
gifts['fourth'] = 'calling birds';
assert(gifts.length == 2);
如果map对象是一个编译常量,不可以对map进行修改。
final constantMap = const {
2: 'helium',
10: 'neon',
18: 'argon',
};
// constantMap[2] = 'Helium'; // Uncommenting this causes an error.
Runes
In Dart, runes are the UTF-32 code points of a string.
在Dart中,runes是UTF-32编码的字符。Unicode对每个字符,数字,标记都定义了一个唯一编码。因为Dart的字符串是UTF-16编码,如果希望表示32位的字符编码,需要特殊写法。一般的unicode编码写法是\uxxxx,xxxx是4个16进制字符。例如, (♥) is \u2665.如果编码是多于或者少于4个字符,需要用大括号分割,比如 (😆) is \u{1f600}。
Symbols (看不太懂)
A Symbol object represents an operator or identifier declared in a Dart program. You might never need to use symbols, but they’re invaluable for APIs that refer to identifiers by name, because minification changes identifier names but not identifier symbols.
To get the symbol for an identifier, use a symbol literal, which is just # followed by the identifier:
#radix
#bar
Symbol literals are compile-time constants.
- 第三篇准备翻译 Functions 函数