Skip to content

Instantly share code, notes, and snippets.

View eernstg's full-sized avatar

Erik Ernst eernstg

  • Google
  • Aarhus, Denmark
View GitHub Profile
const count = 1000000;
bool b = false;
dynamic top;
void main() {
var sw = Stopwatch();
sw.start();
for (int i = 0; i < count; ++i) {
@eernstg
eernstg / selective_access.dart
Last active January 4, 2024 14:59
Example showing selective access to a private interface using extension types
// Main idea is "same object, different treatment". We create one
// `_Buffer` object and use it as a `WriteBuffer` or as a `ReadBuffer`.
// The two perspectives on the `_Buffer` yield unrelated types,
// different members, even different variance of the type parameter.
// A client _must_ use one or the other extension type, there is no
// way a caller outside this library can directly call any instance
// member of `_Buffer`.
class _Buffer<X> {
X? _value;
import 'dart:async';
typedef BetterCompleter<T> = ({
void Function(T) complete,
void Function(Object, [StackTrace?]) completeError,
bool Function() isCompleted
});
BetterCompleter<T> fromCompleter<T>(Completer<T> c) => (
complete: c.complete,
import 'dart:async';
typedef Inv<X> = X Function(X);
typedef BetterCompleter<X> = _BetterCompleter<X, Inv<X>>;
extension type _BetterCompleter<X, Invariance extends Inv<X>>
.fromCompleter(Completer<X> _) {
void complete(X value) => _.complete(value);
void completeError(Object error, [StackTrace? stackTrace]) =>
_.completeError(error, stackTrace);
@eernstg
eernstg / handle-unstable-flutter-getters.txt
Created November 1, 2023 21:46
List of locations where `avoid_unstable_final_fields` emitted a warning, with a comment on each location about how to handle it.
dev/conductor/core/test/common.dart:79:11
`String? name;` could just as well be `final String? name;`
dev/conductor/core/test/common.dart:82:15
`command` could just as well be `final`
dev/conductor/core/test/common.dart:88:20
Could return `const <String>[]`.
examples/layers/rendering/src/binding.dart:39:18
Could be `late final RenderView _renderView;``
packages/flutter/lib/src/cupertino/route.dart:391:12
`_page` can be stable if `settings` is stable
@eernstg
eernstg / main.dart
Last active October 12, 2023 10:31
class A<X extends A<X>> {}
class B extends A<B> {}
class C extends B {}
void f<X extends A<X>>(X x) {}
void main() {
f<B>(C()); // OK.
f(C()); // Compile-time error: "Couldn't infer ..".
}
@eernstg
eernstg / pkginfo_extension_type.dart
Last active July 28, 2023 16:01
A variant of the 'Dart decoding using dart:convert' example using extension types
import 'package:http/http.dart' as http;
import 'dart:convert';
main() async {
const pubUrl = "https://pub.dartlang.org/api/packages/protobuf";
var response = await http.get(Uri.parse(Uri.encodeFull(pubUrl)));
if (response.statusCode == 200) {
PkgInfo info = PkgInfo(json.decode(response.body));
print('Package ${info.name}, v ${info.latest.pubspec.version}');
} else {
@eernstg
eernstg / pkginfo_inline.dart
Created January 30, 2023 19:01
A variant of the 'Dart decoding using dart:convert' example using inline classes
import 'package:http/http.dart' as http;
import 'dart:convert';
main() async {
const pubUrl = "https://pub.dartlang.org/api/packages/protobuf";
var response = await http.get(Uri.parse(Uri.encodeFull(pubUrl)));
if (response.statusCode == 200) {
PkgInfo info = PkgInfo(json.decode(response.body));
print('Package ${info.name}, v ${info.latest.pubspec.version}');
} else {
@eernstg
eernstg / simplify_conditions.dart
Last active August 1, 2022 12:56
Illustrate how link-time sets or maps could be used to reduce the size of conditions based on tree-shaking
// Some code in this file depends on the 'link-time sets and maps' feature,
// cf. https://github.com/dart-lang/language/issues/371. The basic idea is that
// a link-time set/map can be declared in one library L, and any library L1 that
// imports L can contain contributions to said set/map (you can do `add` on a
// `Set` and `[]=` on a `Map`), and those operations occur before run time.
// For each of the contributions (`add` or `[]=` on a link time collection), the
// operation can be made conditional on the existence of some other entity (or,
// in general, on a boolean expression whose basic predicates is those entities).
// For instance, we could have `myLinkTimeSet.add(5) if C;`, which means that `5`
// will be added to `myLinkTimeSet` if and only if the class `C` is still contained
enum Key {
key1,
key2;
T mapFor<T>({
required T Function() key1,
required T Function() key2,
}) {
switch (this) {
case Key.key1: return key1();