1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
|
/// Global
class Point { double x; double y; }
/// PASS
assert abs(p.x - 1) < 0.01 && abs(p.y) < 0.01;
/// Toplevel
/* Use the custom constructor before its declaration. */
let Point p = new Point(angle: 0, distance: 1);
new Point(double angle, double distance)
{ this(x: distance * cos(angle), y: distance * sin(angle)); }
/// PASS
/// Toplevel
/* Use the custom constructor before the declaration of the class. */
let Point2 origin = new Point2(angle: 0, distance: 0);
class Point2 { double x; double y; }
new Point2(double angle, double distance)
{ this(x: distance * cos(angle), y: distance * sin(angle)); }
/// PASS
let p = new Point((0,1));
assert abs(p.x - 1) < 0.01 && abs(p.y) < 0.01;
/// Toplevel
new Point(double angle, double distance)
{ this(x: distance * cos(angle), y: distance * sin(angle)); }
new Point((double angle, double distance) polarCoordinates)
{
// Forward to another custom constructor.
this(angle: angle, distance: distance);
}
/// PASS
var p = new Point(angle: 0, distance: 1);
assert abs(p.x - 1) < 0.01 && abs(p.y) < 0.01;
p = new ColoredPoint(angle: 0, distance: 1, color: "red");
assert abs(p.x - 1) < 0.01 && abs(p.y) < 0.01;
/// Toplevel
new Point(double angle, double distance)
{ this(x: distance * cos(angle), y: distance * sin(angle)); }
class ColoredPoint extends Point { String color; }
/// PASS
/// package a
/// Toplevel
new Point(double xy) = this(x: xy, y : xy);
/// package b import a;
let p = new Point(xy: 10.11);
/// PASS
/// package a
/// Toplevel
new Point(double angle, double distance)
{ this(x: distance * cos(angle), y: distance * sin(angle)); }
/// package b import a;
let p = new Point(angle: 0, distance: 1);
/// PASS
/// package a
/// Toplevel
class Point2 { double x; double y; }
/// package b import a;
let p = new Point2(angle: 0, distance: 1);
/// Toplevel
new Point2(double angle, double distance)
{ this(x: distance * cos(angle), y: distance * sin(angle)); }
/// FAIL
/// Toplevel
new /*/// FAIL HERE */ String(int x) = this("xyz");
/// FAIL
/// Toplevel
new /*/// FAIL HERE */ FooBar(int x) = this("xyz");
/// PASS
/// package a
A<String> a = new A(x: 5, z: "abc");
assert a.t.equals("abc");
/// Toplevel
class A<T>
{
String str;
T t;
}
// Locally rename the type parameter
<U> new A(int x, U z) = this(str: x.toString(), t: z);
/// package b import a
A<String> a = new A(x: 5, z: "abc");
A<String> b = new A(str: "5", t: "abc");
/// PASS
// bug #969777
let b = new B();
// Usage of closure inside a custom constructor
/// Toplevel
class A {
void foo() {}
}
class B extends A {
void fooB() = this.foo();
}
void fst(int i) {}
new B(int x) {
[x].foreach(fst);
this();
}
/// FAIL
/// Toplevel
class A {}
new /*/// FAIL HERE */A(int n) {
this(n);
}
/// PASS
// bug #1070579
let x = new X(5);
/// Toplevel
class X {}
new X(int a) {
int n = 0;
void foo() { n++; }
foo();
this();
}
/// PASS
// bug #1072970
/// Toplevel
class X {
String str;
}
new X() = this(str: "abc");
/// PASS
/// Toplevel
class A {}
new A(int) { this(); }
/// FAIL
let x = /*/// FAIL HERE */ new A();
/// Toplevel
class A {
int x;
String s = "abc";
}
|