Skip to content

Instantly share code, notes, and snippets.

View eernstg's full-sized avatar

Erik Ernst eernstg

  • Google
  • Aarhus, Denmark
View GitHub Profile
@eernstg
eernstg / traits_as_capable_type_objects.dart
Last active February 25, 2025 14:52
'Traits for Dart': How would we do it using 'more capable type objects'? See the first comment for some background.
/*
This is the basic approach, showing how the examples in 'Traits for Dart'
can be written using 'More capable type objects'. The main differences are
that the 'traits' approach can add an implementation to an existing type
(say, `String`) whereas the 'type objects' approach supports late binding.
*/
// --- Glue classes.
class Writer {
static extension EdgeInsetsGeometryExtensions on EdgeInsetsGeometry {
// Forward to EdgeInsets:
const factory EdgeInsetsGeometry.fromLTRB = EdgeInsets.fromLTRB;
const factory EdgeInsetsGeometry.all = EdgeInsets.all;
const factory EdgeInsetsGeometry.only = EdgeInsets.only;
const factory EdgeInsetsGeometry.symmetric = EdgeInsets.symmetric;
static const zero = EdgeInsets.only();
// Forward to EdgeInsetsDirectional:
static extension EdgeInsetsGeometryExtensions on EdgeInsetsGeometry {
// Forward to EdgeInsets:
const factory EdgeInsetsGeometry.fromLTRB(
double left,
double top,
double right,
double bottom,
) = EdgeInsets.fromLTRB;
const factory EdgeInsetsGeometry.all(double value) = EdgeInsets.all;
@eernstg
eernstg / detail_page.dart
Last active August 14, 2024 19:07
Variant of detail_page.dart that uses several hypothetical features of Dart to reduce verbosity
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart' show Divider;
import 'package:landmarks/widgets/favorite_button.dart';
import '../model/landmarks_model.dart';
import '../model/landmark.dart';
import '../widgets/circle_image.dart';
import '../widgets/map_view.dart';
extension on BuildContext {
@eernstg
eernstg / stateful_boilerplate_plain.dart
Last active July 19, 2024 13:31
Example from 'Cutting the stateful boilerplate' using likely Dart features (primary constructors, inferred `required`, `override`)
import 'package:flutter/material.dart';
// Assumptions:
// * Dart has primary constructors.
// * Dart supports `override`, and override inference
// is considered appropriate.
// * The named parameter `String name` can omit `required`
// because it is inferred.
void main() {
@eernstg
eernstg / stateful_boilerplate_macro.dart
Last active May 14, 2024 20:29
Example from 'Cutting the stateful boilerplate' using the proposed macros, such as `@Stateful`
import 'package:flutter/material.dart';
void main() {
runApp(const App(name: 'Dash'));
}
@Stateless()
class App {
@Input() String get name;
import 'package:http/http.dart' as http;
import 'dart:convert';
import 'package:json/json.dart';
main() async {
const pubUrl = "https://pub.dartlang.org/api/packages/protobuf";
var response = await http.get(Uri.parse(pubUrl));
if (response.statusCode == 200) {
PkgInfo info = PkgInfo.fromJson(json.decode(response.body));
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,