Skip to content

Instantly share code, notes, and snippets.

@hugovdm
hugovdm / main.dart
Last active October 18, 2018 13:29
Dart Assertion not failing
class A {
const A(this.x) : assert(x != null);
final String x;
String toString() => 'A: $x';
}
main() {
// Why does this not cause an assertion failure?
A a = A(null);
print(a);
@hugovdm
hugovdm / main.dart
Created September 21, 2018 15:43
Dart's operator [] supports more than just int
class Idx {
Idx(this.val);
int val;
}
class S {
final List<int> _list = <int>[];
add(int value) => _list.add(value);
remove(int index) => _list.removeAt(index);
class MyImmutable {
final String _x;
const MyImmutable([String x])
: _x = (x != null) ? x.length > 0 ? x : null : null;
String toString() => 'MyImmutable("$_x")';
}
void main() {
// These work:
print(const MyImmutable('a'));
String fmtCompares(double a, double b) {
return '${a < b}, ${a == b}, ${a > b}, ${a.compareTo(b)}';
}
void main() {
double nan = 0 / 0;
double negZero = -0.0;
var l = [-1.0, 0.0, 1.0, nan, negZero];
print(l);
l.sort();
@hugovdm
hugovdm / main.dart
Last active August 27, 2018 12:04
Dart Strings and Unicode
main() {
print(
"code units are 2 bytes in UTF-16. In some cases, one needs a surrogate pair of code units to form a code point. And one might need multiple code points, multiple characters, to form a single grapheme (e.g. an emoji).");
print('');
var clapping = '\u{1f44f}';
print(clapping);
print("codeUnits: ${clapping.codeUnits}.");
print("runes, providing code points, not code units: ${clapping.runes}.");