Update constructor syntax (#913)

This commit is contained in:
josh11b
2021-10-25 13:05:39 -07:00
committed by GitHub
parent 3bec7f8dc0
commit 8d051e2b6b
+19 -19
View File
@@ -224,10 +224,10 @@ class Point {
impl as Vector {
// In this scope, "Self" is an alias for "Point".
fn Add[me: Self](b: Self) -> Self {
return Point(.x = a.x + b.x, .y = a.y + b.y);
return {.x = a.x + b.x, .y = a.y + b.y};
}
fn Scale[me: Self](v: Double) -> Self {
return Point(.x = a.x * v, .y = a.y * v);
return {.x = a.x * v, .y = a.y * v};
}
}
}
@@ -236,8 +236,8 @@ class Point {
Interfaces that are implemented inline contribute to the type's API:
```
var p1: Point = (.x = 1.0, .y = 2.0);
var p2: Point = (.x = 2.0, .y = 4.0);
var p1: Point = {.x = 1.0, .y = 2.0};
var p2: Point = {.x = 2.0, .y = 4.0};
Assert(p1.Scale(2.0) == p2);
Assert(p1.Add(p1) == p2);
```
@@ -263,7 +263,7 @@ meaning their data representations are the same, so we allow you to convert
between the two freely:
```
var a: Point = (.x = 1.0, .y = 2.0);
var a: Point = {.x = 1.0, .y = 2.0};
// `a` has `Add` and `Scale` methods:
a.Add(a.Scale(2.0));
@@ -376,10 +376,10 @@ class Point2 {
external impl Point2 as Vector {
// In this scope, "Self" is an alias for "Point2".
fn Add[me: Self](b: Self) -> Self {
return Point2(.x = a.x + b.x, .y = a.y + b.y);
return {.x = a.x + b.x, .y = a.y + b.y};
}
fn Scale[me: Self](v: Double) -> Self {
return Point2(.x = a.x * v, .y = a.y * v);
return {.x = a.x * v, .y = a.y * v};
}
}
```
@@ -409,7 +409,7 @@ On the other hand, if we convert to the facet type, those methods do become
visible:
```
var a: Point2 = (.x = 1.0, .y = 2.0);
var a: Point2 = {.x = 1.0, .y = 2.0};
// `a` does *not* have `Add` and `Scale` methods:
// ❌ Error: a.Add(a.Scale(2.0));
@@ -435,14 +435,14 @@ class Point3 {
var x: Double;
var y: Double;
fn Add[me: Self](b: Self) -> Self {
return Point3(.x = a.x + b.x, .y = a.y + b.y);
return {.x = a.x + b.x, .y = a.y + b.y};
}
}
external Point3 as Vector {
external impl Point3 as Vector {
alias Add = Point3.Add; // Syntax TBD
fn Scale[me: Self](v: Double) -> Self {
return Point3(.x = a.x * v, .y = a.y * v);
return {.x = a.x * v, .y = a.y * v};
}
}
```
@@ -482,8 +482,8 @@ _qualified name_, whether or not the implementation is done externally with an
`external impl` statement:
```
var p1: Point2 = (.x = 1.0, .y = 2.0);
var p2: Point2 = (.x = 2.0, .y = 4.0);
var p1: Point2 = {.x = 1.0, .y = 2.0};
var p2: Point2 = {.x = 2.0, .y = 4.0};
Assert(p1.(Vector.Scale)(2.0) == p2);
Assert(p1.(Vector.Add)(p1) == p2);
```
@@ -510,7 +510,7 @@ You could access `Draw` with a qualified name:
import Plot;
import Points;
var p: Points.Point2 = (.x = 1.0, .y = 2.0);
var p: Points.Point2 = {.x = 1.0, .y = 2.0};
p.(Plot.Drawable.Draw)();
```
@@ -582,8 +582,8 @@ Even though `Point2` doesn't have `Add` and `Scale` methods, it still implements
`Vector` and so can still call `AddAndScaleGeneric`:
```
var a2: Point2 = (.x = 1.0, .y = 2.0);
var w2: Point2 = (.x = 3.0, .y = 4.0);
var a2: Point2 = {.x = 1.0, .y = 2.0};
var w2: Point2 = {.x = 3.0, .y = 4.0};
var v3: Point2 = AddAndScaleGeneric(a, w, 2.5);
```
@@ -630,10 +630,10 @@ var VectorForPoint: Vector = {
// `lambda` is **placeholder** syntax for defining a
// function value.
.Add = lambda(a: Point, b: Point) -> Point {
return Point(.x = a.x + b.x, .y = a.y + b.y);
return {.x = a.x + b.x, .y = a.y + b.y};
},
.Scale = lambda(a: Point, v: Double) -> Point {
return Point(.x = a.x * v, .y = a.y * v);
return {.x = a.x * v, .y = a.y * v};
},
};
```
@@ -1518,7 +1518,7 @@ or using qualified names:
adapter SongByTitle for Song {
impl as Comparable {
fn Less[me: Self](that: Self) -> bool {
return this.(Song.Title)() < that(Song.Title)();
return this.(Song.Title)() < that.(Song.Title)();
}
}
}