Skip to content

Instantly share code, notes, and snippets.


Erik Ernst eernstg

  • Google
  • Aarhus, Denmark
View GitHub Profile
eernstg / pkginfo_inline.dart
Created January 30, 2023 19:01
A variant of the 'Dart decoding using dart:convert' example using inline classes
View pkginfo_inline.dart
import 'package:http/http.dart' as http;
import 'dart:convert';
main() async {
const pubUrl = "";
var response = await http.get(Uri.parse(Uri.encodeFull(pubUrl)));
if (response.statusCode == 200) {
PkgInfo info = PkgInfo(json.decode(response.body));
print('Package ${}, v ${info.latest.pubspec.version}');
} else {
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
View simplify_conditions.dart
// Some code in this file depends on the 'link-time sets and maps' feature,
// cf. 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
View main.dart
enum Key {
T mapFor<T>({
required T Function() key1,
required T Function() key2,
}) {
switch (this) {
case Key.key1: return key1();
View receiver_typeclass_example.dart
// ----------------------------------------------------------------------
// Basic example, hypothetical code only.
// typeclass Index<inout X, in Y> {
// X X.operator [](Y y);
// }
// instance Index<int, int> {
// int int.operator [](int y) => this + y;
// }
// define an example struct, make it printable
struct Foo;
// an example trait
trait Bar {
fn baz(&self);
// implement the trait for Foo
eernstg / typeclass_example.dart
Last active March 24, 2022 14:45
Typeclass example
View typeclass_example.dart
Several examples, separated by '-----' lines.
We probably want to use a regular type variable declaration list plus
a constraint specification list (to request a dispatcher object for a
specific typeclass instance), so I'm using `where` clauses in various
Dispatcher objects are holders of functions, and there is no particular
notion of a receiver, so we're likely to benefit _greatly_ from sound
eernstg / swiftUI.dart
Last active December 17, 2021 00:46
Illustrating the effect of making parameters named `child` and `children` optionally named (and using `named-parameters-anywhere`).
View swiftUI.dart
void main() {
height: 57,
if (widget.icon != null)
SizedBox(height: 29, width: 29, widget.icon),
const SizedBox(height: 8.5),
View delegate.dart
abstract class Person {
String name;
String address;
class DelegatorPerson implements Person {
DelegateePerson delegatee;
String get name =>;
set name(String value) => delegatee.nameSet(this, value);
eernstg / main.dart
Last active November 4, 2019 14:45
Code for comment on 'How language features affect OO design'
View main.dart
main() {
new Dog().feed("some food");
abstract class Pet {
void feed(String food);
void pet();
class Dog = Pet with FeedableDog, PettableDog;
eernstg /
Last active August 14, 2018 13:37
Dart Feature Specification: Evaluating integer literals as double values