1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import 'package:ch01_dart_basics/basics.dart';
void main() {
print('--- 1. 数字 ---');
print('int 溢出回绕: ${intOverflow()}');
print('7 ~/ 2 = ${truncatingDivision(7, 2)}, 7 / 2 = ${doubleDivision(7, 2)}');
print('-7 % 3 = ${moduloFollowsDivisor(-7, 3)} (C/Go 给 -1)');
print('7 % -3 = ${moduloFollowsDivisor(7, -3)} · 7.remainder(-3) = ${cStyleRemainder(7, -3)}');
print('0.1 + 0.2 == 0.3 ? ${floatSumIsExact()}');
print('abs(-3.5) = ${absOf(-3.5)}, abs(-42) = ${absOf(-42)}');
print('0xFF + 0x0A = ${hexLiteral()} (Dart 无二进制字面量)');
print('\n--- 2. 字符串 ---');
print(greet('Gopher', 30));
print('raw: ${windowsPath()}');
print(banner().trimRight());
const emoji = '👋 Dart';
print('"$emoji" code units = ${codeUnitLength(emoji)}, runes = ${runeLength(emoji)}');
print('\n--- 3. const / final ---');
print('maxRetries = $maxRetries, defaultPorts = $defaultPorts');
print('const 列表被规范化: ${constListsAreCanonicalized()}');
print('普通列表没有结构相等: ${plainListsAreNotEqual()}');
print('final 每次都是新对象: ${requestTimestamp()} / ${requestTimestamp()}');
print('\n--- 4. 函数 ---');
print(buildUser(name: 'alice'));
print(buildUser(name: 'bob', age: 42, tag: 'admin', active: false));
print('sumAll([1,2,3]) = ${sumAll([1, 2, 3])}');
print('sumAll([1,2,3], 100) = ${sumAll([1, 2, 3], 100)}');
print('sumSpread = ${sumSpread([1, 2], [3, 4])}');
print('\n--- 5. 函数作为值 / 闭包 ---');
print('applyOp(add, 3, 4) = ${applyOp(add, 3, 4)}');
print('applyOp(mul, 3, 4) = ${applyOp(mul, 3, 4)}');
print('addThenMul(3, 4) = ${addThenMul(3, 4)}');
final acc = makeAccumulator(start: 10);
print('accumulator: ${acc(1)}, ${acc(2)}, ${acc(3)}');
print('循环变量捕获: ${capturedLoopVariables(3)}');
print('\n--- 6. record ---');
final (:quotient, :remainder) = divMod(17, 5);
print('divMod(17, 5) = ($quotient, $remainder)');
print('divMod(17,5) == (3,2) ? ${divMod(17, 5) == (quotient: 3, remainder: 2)}');
print('minMax([3,1,4,1,5]) = ${minMax([3, 1, 4, 1, 5])}');
print('\n--- 7. 静态类型 / dynamic ---');
print(inferredTypeOf(1));
print(dynamicDispatch(3.14));
}