Skip to content

Instantly share code, notes, and snippets.

Created August 20, 2015 00:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/0cb25997742ed5382e4a to your computer and use it in GitHub Desktop.
Save anonymous/0cb25997742ed5382e4a to your computer and use it in GitHub Desktop.
bitter-dawn-5945

bitter-dawn-5945

This gigantic Dart program contains some errors and warnings. This code has no associated html or css. It imports the 'dart:math' and 'dart:convert' packages as well.

Created with <3 with dartpad.dartlang.org.

import 'dart:math' show sqrt;
import 'dart:convert' show JSON;
class Point {
num x;
num y;
// Syntactic sugar for setting x and y
// before the constructor body runs.
Point(this.x, this.y);
// Initializer list sets instance variables before the constructor body runs.
Point.fromJson(Map jsonMap)
: x = jsonMap['x'],
y = jsonMap['y'] {
print('In Point.fromJson() constructor: $this');
}
num distanceTo(Point other) {
print('calculating distance between $this and $other');
var dx = x - other.x;
var dy = y - other.y;
return sqrt(dx * dx + dy * dy);
}
String toString() => '($x, $y)';
}
class ImmutablePoint {
final num x;
final num y;
const ImmutablePoint(this.x, this.y);
static final ImmutablePoint origin =
const ImmutablePoint(0, 0);
}
main() {
var jsonData = JSON.decode('{"x":1, "y":2}');
// Create a Point using Point().
var p1 = new Point(2, 2);
// Create a Point using Point.fromJson().
var p2 = new Point.fromJson(jsonData);
var p = new Point(2, 2);
// Set the value of the instance variable y.
p.y = 3;
// Get the value of y.
assert(p.y == 3);
// Invoke distanceTo() on p.
num distance = p.distanceTo(new Point(4, 4));
print(distance);
// Now do the same with ?.
var p3 = new Point(4, 3);
// Set the value of the instance variable y.
p3?.y = 4;
// Get the value of y.
assert(p3?.y == 4);
// Invoke distanceTo() on p3.
num d3 = p3?.distanceTo(p);
print(d3);
/*
* TODO: make this testable
query('#button')
..text = 'Click to Confirm' // Get an object. Use its
..classes.add('important') // instance variables
..onClick.add((e) => window.alert('Confirmed!')); // and methods.
*/
/* var */ p = const ImmutablePoint(2, 2);
var a = const ImmutablePoint(1, 1);
var b = const ImmutablePoint(1, 1);
assert(identical(a, b)); // They are the same instance!
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment