Skip to content

Instantly share code, notes, and snippets.

View eernstg's full-sized avatar

Erik Ernst eernstg

  • Google
  • Aarhus, Denmark
View GitHub Profile
// Only supported in the analyzer at this time.
// Use `--enable-experiment=anonymous-methods`.
// ----------------------------------------------------------------------
// Example from Sam Rawlins, discussed in chat.
// Original code.
Bucket? get _daysToFirstConversion1 =>
(_forecastRpcState?.performanceForecast?.hasDaysToFirstConversion() ??
@eernstg
eernstg / monoid.dart
Last active September 10, 2025 08:19
Emulate the monoid example from the talk by Brian Goetz about Java type classes (JVMLS 2025)
// This library shows how metaobjects in Dart can be used to achieve a
// similar effect as the Java type class proposal in the talk by Brian
// Goetz. The point is that we can use types (no need to have an instance)
// to work with "static-like" properties like the `zero` value and the
// `plus` operation of a `Monoid`.
//
// No attempt is made to include the syntactic sugar than allows
// `witness.plus(a, b)` to be written as `a + b`. That's a separate
// consideration, and it might be useful with metaobjects as well as
// in other contexts. So that's a topic for another day, and the code
@eernstg
eernstg / text_primary_body_abbreviated.dart
Last active July 28, 2025 16:27
Using parameter list abstraction to make both `Text` and `Text.rich` redirect to a shared `Text._` constructor.
/// A run of text with a single style.
///
/// The [Text] widget displays a string of text with single style. The string
/// might break across multiple lines or might all be displayed on the same line
/// depending on the layout constraints.
///
/// The [style] argument is optional. When omitted, the text will use the style
/// from the closest enclosing [DefaultTextStyle]. If the given style's
/// [TextStyle.inherit] property is true (the default), the given style will
/// be merged with the closest enclosing [DefaultTextStyle]. This merging
// This program printed `A<List<int>, dynamic>` before inference-using-bounds.
// With that feature it prints `A<List<int>, int>`.
class A<X extends Iterable<Y>, Y> {
final X x;
A(this.x) {
print('Creating an A<$X, $Y>.');
}
Y get first => x.first;
}
@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;