|
| 1 | +// Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file |
| 2 | +// for details. All rights reserved. Use of this source code is governed by a |
| 3 | +// BSD-style license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +/// An example with more complicated scope |
| 6 | +import 'dart:async'; |
| 7 | +import 'dart:collection'; |
| 8 | + |
| 9 | +final libraryPublicFinal = MyTestClass(); |
| 10 | + |
| 11 | +final _libraryPrivateFinal = 1; |
| 12 | +Object? libraryNull; |
| 13 | +var libraryPublic = <String>['library', 'public', 'variable']; |
| 14 | +var notAList = NotReallyAList(); |
| 15 | + |
| 16 | +var _libraryPrivate = ['library', 'private', 'variable']; |
| 17 | + |
| 18 | +var identityMap = <String, int>{}; |
| 19 | + |
| 20 | +var map = <Object, Object>{}; |
| 21 | + |
| 22 | +void staticFunction(int formal) { |
| 23 | + print(formal); // Breakpoint: staticFunction |
| 24 | +} |
| 25 | + |
| 26 | +void main() async { |
| 27 | + print('Initial print from scopes app'); |
| 28 | + var local = 'local in main'; |
| 29 | + var intLocalInMain = 42; |
| 30 | + var testClass = MyTestClass(); |
| 31 | + Object? localThatsNull; |
| 32 | + identityMap['a'] = 1; |
| 33 | + identityMap['b'] = 2; |
| 34 | + map['a'] = [1, 2, 3]; |
| 35 | + map['b'] = 'something'; |
| 36 | + notAList.add(7); |
| 37 | + |
| 38 | + String nestedFunction<T>(T parameter, Object aClass) { |
| 39 | + var another = int.tryParse('$parameter'); |
| 40 | + return '$local: parameter, $another'; // Breakpoint: nestedFunction |
| 41 | + } |
| 42 | + |
| 43 | + dynamic nestedWithClosure(String banana) { |
| 44 | + return () => '$local + $banana'; |
| 45 | + } |
| 46 | + |
| 47 | + Timer.periodic(const Duration(seconds: 1), (Timer t) { |
| 48 | + var ticks = t.tick; |
| 49 | + // ignore: unused_local_variable, prefer_typing_uninitialized_variables |
| 50 | + var closureLocal; |
| 51 | + libraryPublicFinal.printCount(); |
| 52 | + staticFunction(1); |
| 53 | + print('ticking... $ticks (the answer is $intLocalInMain)'); |
| 54 | + print(nestedFunction('$ticks ${testClass.message}', Timer)); |
| 55 | + print(localThatsNull); |
| 56 | + print(libraryNull); |
| 57 | + var localList = libraryPublic; |
| 58 | + print(localList); |
| 59 | + localList.add('abc'); |
| 60 | + var f = testClass.methodWithVariables(); |
| 61 | + print(f('parameter')); |
| 62 | + }); |
| 63 | + |
| 64 | + print(_libraryPrivateFinal); |
| 65 | + print(_libraryPrivate); |
| 66 | + print(nestedFunction(_libraryPrivate.first, Object)); |
| 67 | + print(nestedWithClosure(_libraryPrivate.first)()); |
| 68 | +} |
| 69 | + |
| 70 | +String libraryFunction(String arg) { |
| 71 | + print('calling a library function with $arg'); |
| 72 | + var concat = 'some constant plus $arg plus whatever'; |
| 73 | + print(concat); |
| 74 | + return concat; |
| 75 | +} |
| 76 | + |
| 77 | +class MyTestClass<T> { |
| 78 | + final String message; |
| 79 | + |
| 80 | + late String notFinal; |
| 81 | + |
| 82 | + MyTestClass({this.message = 'world'}) { |
| 83 | + myselfField = this; |
| 84 | + tornOff = toString; |
| 85 | + } |
| 86 | + |
| 87 | + String hello() => message; |
| 88 | + |
| 89 | + String Function(String) methodWithVariables() { |
| 90 | + var local = '$message + something'; |
| 91 | + print(local); |
| 92 | + return (String parameter) { |
| 93 | + // Be sure to use a field from this, so it isn't entirely optimized away. |
| 94 | + var closureLocalInsideMethod = '$message/$local/$parameter'; |
| 95 | + print(closureLocalInsideMethod); |
| 96 | + return closureLocalInsideMethod; // Breakpoint: nestedClosure |
| 97 | + }; |
| 98 | + } |
| 99 | + |
| 100 | + //ignore: avoid_returning_this |
| 101 | + MyTestClass get myselfGetter => this; |
| 102 | + |
| 103 | + late MyTestClass myselfField; |
| 104 | + |
| 105 | + var count = 0; |
| 106 | + |
| 107 | + // An easy location to add a breakpoint. |
| 108 | + void printCount() { |
| 109 | + print('The count is ${++count}'); |
| 110 | + libraryFunction('abc'); // Breakpoint: printMethod |
| 111 | + } |
| 112 | + |
| 113 | + final _privateField = 'a private field'; |
| 114 | + |
| 115 | + // ignore: unused_element |
| 116 | + String privateMethod(String s) => '$s : $_privateField'; |
| 117 | + |
| 118 | + @override |
| 119 | + String toString() => 'A test class with message $message'; |
| 120 | + |
| 121 | + bool equals(Object other) { |
| 122 | + if (other is MyTestClass) return message == other.hello(); |
| 123 | + return false; |
| 124 | + } |
| 125 | + |
| 126 | + Function closure = someFunction; |
| 127 | + |
| 128 | + late String Function() tornOff; |
| 129 | +} |
| 130 | + |
| 131 | +Function? someFunction() => null; |
| 132 | + |
| 133 | +// ignore: unused_element |
| 134 | +int _libraryPrivateFunction(int a, int b) => a + b; |
| 135 | + |
| 136 | +class NotReallyAList extends ListBase { |
| 137 | + final List<Object?> _internal; |
| 138 | + |
| 139 | + NotReallyAList() : _internal = []; |
| 140 | + |
| 141 | + @override |
| 142 | + Object? operator [](x) => _internal[x]; |
| 143 | + |
| 144 | + @override |
| 145 | + operator []=(x, y) => _internal[x] = y as Object?; |
| 146 | + |
| 147 | + @override |
| 148 | + int get length => _internal.length; |
| 149 | + |
| 150 | + @override |
| 151 | + set length(x) => _internal.length = x; |
| 152 | +} |
0 commit comments