Skip to content

Instantly share code, notes, and snippets.

@lrhn
lrhn / build_extension_sample.dart
Last active November 7, 2019 10:53
Example of extension of built values list-builder.
abstract class Built<V extends Built<V, B>, B extends Builder<V, B>> {
V rebuild(void Function(B builder) edit);
}
abstract class Builder<V extends Built<V, B>, B extends Builder<V, B>> {
V build();
}
abstract class ListBuilder<V> {
V operator [](int index);
void operator []=(int index, V element);
}
@lrhn
lrhn / trim_leading_whitespace_benchmark.dart
Last active September 6, 2019 12:39
Dart Trim Leading Whitespace Benchmark
final RegExp _commonLeadingWhitespaceRE =
RegExp(r"(?=[^]*^([ \t]+)$(?![^]))(?![^]*^(?!\1))", multiLine: true);
String trimLeadingWhitespaceRE(String text) {
var commonWhitespace = _commonLeadingWhitespaceRE.matchAsPrefix(text);
if (commonWhitespace != null) {
return text.replaceAll(
RegExp("^${commonWhitespace[1]}", multiLine: true), "");
}
return text;
@lrhn
lrhn / await_benchmark.dart
Created July 30, 2019 16:39
Await Benchrmark
import "dart:async";
const NeverInline = "NeverInline";
Future<double> measureForSync(void fun()) async {
int c = 0;
int e = 0;
final sw = Stopwatch()..start();
do {
fun();
@lrhn
lrhn / ulp.dart
Created July 1, 2019 13:33
Function returning the ULP of a double number - the value of its least signfiicant bit position.
import "dart:typed_data";
final _ulpBuffer = ByteData(8);
/// The unit of least precision for a [double] number.
///
/// A double number has 53 bits of precision. The unit of least precision (ULP)
/// is the value of the least of those bits.
/// The returned ULP is always positive.
double ulp(double d) {
var buffer = _ulpBuffer;
@lrhn
lrhn / canceller_stream.dart
Created May 28, 2019 08:25
Stream Wrapper Allowing Cancel On The Side
import "dart:async";
/// Adapts a stream so that its subscription can be cancelled from the outside.
///
/// The output [stream] provides the same events as a specified source stream,
/// but the [cancel] operation can cancel the subscription on the source
/// stream and prevent any more events from reaching the output [stream].
abstract class StreamCanceller<T> {
/// Creates a stream canceller for [source].
///
@lrhn
lrhn / CSharp.cs
Last active April 11, 2019 10:39
Example of Extension Method Resolution
// C# has invariant generics, so only `b.Copy()` matches the `Bar.Copy` method.
// It's unsurprising that the other two use Foo.
// For the conflict, C# prefers the non-generic function over the generic one.
// That's also true for normal static class.
using System;
using System.Collections.Generic;
public class Program
{
@lrhn
lrhn / null-means-optional.md
Created April 2, 2019 11:48
Nullable Parameters are Optional

Required Parameters

Dart has positional and named parameters, and positional parameters can be required or optional. Named parameters are always optional, but users want to have required named parameters too.

With NNBD, in optional non-nullable parameter with no default value does not make sense. If you call the function without a corresponding argument, there is no natural value to ascribe to the parameter. The parameter is, effectively, required. We'd like to disallow invalid calls statically, so this is important at the static type level as well.

This document attempts to solve these two issues (adding required named parameters, handling optional parameters with a non-nullable type and no default value in some way), and do so in a way that is consistent between named and positional parameters, and which uses a minimum of extra syntax.

Proposal

@lrhn
lrhn / NNBD Design.md
Last active March 15, 2019 12:56
NNBD Design

NNBD Design

[email protected]

This are intended as a "full" NNBD design.

The Type System

Dart types already include, in both the static and runtime type system, the following type kinds:

@lrhn
lrhn / json_object.dart
Created February 17, 2019 22:04
JSON object wrapper.
import "dart:core";
import "dart:core" as c;
abstract class _JsonBase<Key> {
Object _get(Key key, c.bool allowNull);
List<T> listOf<T>(Key key) => (_get(key, false) as List<Object>).cast<T>();
Map<String, T> mapOf<T>(Key key) =>
(_get(key, false) as Map<String, Object>).cast<String, T>();
JsonObject object(Key key) =>
JsonObject(_get(key, false) as Map<String, Object>);
@lrhn
lrhn / main.dart
Created February 11, 2019 12:55
whereType transformer example
import "dart:async";
StreamTransformer<Null, T> whereType<T>() => _WhereTypeTransformer<T>();
class _WhereTypeTransformer<T> extends StreamTransformerBase<Null, T> {
Stream<T> bind(Stream<Object> stream) async* {
await for (var value in stream) if (value is T) yield value;
}
}