Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename me -> self #1382

Merged
merged 22 commits into from
Dec 6, 2022
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 27 additions & 27 deletions docs/design/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1301,22 +1301,22 @@ Class type definitions can include methods:
```carbon
class Point {
// Method defined inline
fn Distance[me: Self](x2: i32, y2: i32) -> f32 {
var dx: i32 = x2 - me.x;
var dy: i32 = y2 - me.y;
fn Distance[self: Self](x2: i32, y2: i32) -> f32 {
var dx: i32 = x2 - self.x;
var dy: i32 = y2 - self.y;
return Math.Sqrt(dx * dx - dy * dy);
}
// Mutating method
fn Offset[addr me: Self*](dx: i32, dy: i32);
fn Offset[addr self: Self*](dx: i32, dy: i32);

var x: i32;
var y: i32;
}

// Out-of-line definition of method declared inline.
fn Point.Offset[addr me: Self*](dx: i32, dy: i32) {
me->x += dx;
me->y += dy;
fn Point.Offset[addr self: Self*](dx: i32, dy: i32) {
self->x += dx;
self->y += dy;
}

var origin: Point = {.x = 0, .y = 0};
Expand All @@ -1328,16 +1328,16 @@ Assert(origin.Distance(3, 4) == 0.0);
This defines a `Point` class type with two integer data members `x` and `y` and
two methods `Distance` and `Offset`:

- Methods are defined as class functions with a `me` parameter inside square
- Methods are defined as class functions with a `self` parameter inside square
brackets `[`...`]` before the regular explicit parameter list in parens
`(`...`)`.
- Methods are called using using the member syntax, `origin.Distance(`...`)`
and `origin.Offset(`...`)`.
- `Distance` computes and returns the distance to another point, without
modifying the `Point`. This is signified using `[me: Self]` in the method
modifying the `Point`. This is signified using `[self: Self]` in the method
declaration.
- `origin.Offset(`...`)` does modify the value of `origin`. This is signified
using `[addr me: Self*]` in the method declaration.
using `[addr self: Self*]` in the method declaration.
- Methods may be declared lexically inline like `Distance`, or lexically out
of line like `Offset`.

Expand Down Expand Up @@ -1481,21 +1481,21 @@ names resolvable by the compiler, and don't act like forward declarations.

A destructor for a class is custom code executed when the lifetime of a value of
that type ends. They are defined with the `destructor` keyword followed by
either `[me: Self]` or `[addr me: Self*]` (as is done with [methods](#methods))
and the block of code in the class definition, as in:
either `[self: Self]` or `[addr self: Self*]` (as is done with
[methods](#methods)) and the block of code in the class definition, as in:

```carbon
class MyClass {
destructor [me: Self] { ... }
destructor [self: Self] { ... }
}
```

or:

```carbon
class MyClass {
// Can modify `me` in the body.
destructor [addr me: Self*] { ... }
// Can modify `self` in the body.
destructor [addr self: Self*] { ... }
}
```

Expand Down Expand Up @@ -2147,7 +2147,7 @@ capabilities that may be assumed of types that satisfy that constraint.
interface Printable {
// Inside an interface definition `Self` means
// "the type implementing this interface".
fn Print[me: Self]();
fn Print[self: Self]();
}
```

Expand All @@ -2170,8 +2170,8 @@ class Circle {
var radius: f32;

impl as Printable {
fn Print[me: Self]() {
Console.WriteLine("Circle with radius: {0}", me.radius);
fn Print[self: Self]() {
Console.WriteLine("Circle with radius: {0}", self.radius);
}
}
}
Expand Down Expand Up @@ -2264,9 +2264,9 @@ associated type to represent the type of elements stored in the stack.
```
interface StackInterface {
let ElementType:! Movable;
fn Push[addr me: Self*](value: ElementType);
fn Pop[addr me: Self*]() -> ElementType;
fn IsEmpty[addr me: Self*]() -> bool;
fn Push[addr self: Self*](value: ElementType);
fn Pop[addr self: Self*]() -> ElementType;
fn IsEmpty[addr self: Self*]() -> bool;
}
```

Expand All @@ -2276,14 +2276,14 @@ values for the `ElementType` member of the interface using a `where` clause:
```carbon
class IntStack {
impl as StackInterface where .ElementType == i32 {
fn Push[addr me: Self*](value: i32);
fn Push[addr self: Self*](value: i32);
// ...
}
}

class FruitStack {
impl as StackInterface where .ElementType == Fruit {
fn Push[addr me: Self*](value: Fruit);
fn Push[addr self: Self*](value: Fruit);
// ...
}
}
Expand Down Expand Up @@ -2311,8 +2311,8 @@ type `T`:

```carbon
class Stack(T:! Type) {
fn Push[addr me: Self*](value: T);
fn Pop[addr me: Self*]() -> T;
fn Push[addr self: Self*](value: T);
fn Pop[addr self: Self*]() -> T;

var storage: Array(T);
}
Expand Down Expand Up @@ -2513,7 +2513,7 @@ types in the `impl` declaration, as in:
```carbon
external impl like T as AddWith(like U) where .Result == V {
// `Self` is `T` here
fn Op[me: Self](other: U) -> V { ... }
fn Op[self: Self](other: U) -> V { ... }
}
```

Expand All @@ -2522,7 +2522,7 @@ implementing the `Add` interface:

```carbon
external impl T as Add {
fn Op[me: Self](other: Self) -> Self { ... }
fn Op[self: Self](other: Self) -> Self { ... }
}
```

Expand Down
72 changes: 36 additions & 36 deletions docs/design/classes.md
Original file line number Diff line number Diff line change
Expand Up @@ -865,25 +865,25 @@ var p2: Point = p1.CreateCentered();

[Method](<https://en.wikipedia.org/wiki/Method_(computer_programming)>)
declarations are distinguished from [class function](#class-functions)
declarations by having a `me` parameter in square brackets `[`...`]` before the
explicit parameter list in parens `(`...`)`. There is no implicit member access
in methods, so inside the method body members are accessed through the `me`
parameter. Methods may be written lexically inline or after the class
declarations by having a `self` parameter in square brackets `[`...`]` before
the explicit parameter list in parens `(`...`)`. There is no implicit member
access in methods, so inside the method body members are accessed through the
`self` parameter. Methods may be written lexically inline or after the class
declaration.

```carbon
class Circle {
fn Diameter[me: Self]() -> f32 {
return me.radius * 2;
fn Diameter[self: Self]() -> f32 {
return self.radius * 2;
}
fn Expand[addr me: Self*](distance: f32);
fn Expand[addr self: Self*](distance: f32);

var center: Point;
var radius: f32;
}

fn Circle.Expand[addr me: Self*](distance: f32) {
me->radius += distance;
fn Circle.Expand[addr self: Self*](distance: f32) {
self->radius += distance;
}

var c: Circle = {.center = Point.Origin(), .radius = 1.5 };
Expand All @@ -895,10 +895,10 @@ Assert(Math.Abs(c.Diameter() - 4.0) < 0.001);
- Methods are called using using the dot `.` member syntax, `c.Diameter()` and
`c.Expand(`...`)`.
- `Diameter` computes and returns the diameter of the circle without modifying
the `Circle` instance. This is signified using `[me: Self]` in the method
the `Circle` instance. This is signified using `[self: Self]` in the method
declaration.
- `c.Expand(`...`)` does modify the value of `c`. This is signified using
`[addr me: Self*]` in the method declaration.
`[addr self: Self*]` in the method declaration.

The pattern '`addr` _patt_' means "first take the address of the argument, which
must be an
Expand All @@ -907,26 +907,26 @@ then match pattern _patt_ against it".

If the method declaration also includes
[deduced generic parameters](/docs/design/generics/overview.md#deduced-parameters),
the `me` parameter must be in the same list in square brackets `[`...`]`. The
`me` parameter may appear in any position in that list, as long as it appears
the `self` parameter must be in the same list in square brackets `[`...`]`. The
`self` parameter may appear in any position in that list, as long as it appears
after any names needed to describe its type.

#### Name lookup in member function definitions

When defining a member function lexically inline, we delay type checking of the
function body until the definition of the current type is complete. This means
that name lookup _for members of objects_ is also delayed. That means that you
can reference `me.F()` in a lexically inline method definition even before the
can reference `self.F()` in a lexically inline method definition even before the
declaration of `F` in that class definition. However, other names still need to
be declared before they are used. This includes unqualified names, names within
namespaces, and names _for members of types_.

```
class Point {
fn Distance[me: Self]() -> f32 {
fn Distance[self: Self]() -> f32 {
// ✅ Allowed: `x` and `y` are names for members of an object,
// and so lookup is delayed until `type_of(me) == Self` is complete.
return Math.Sqrt(me.x * me.x + me.y * me.y);
// and so lookup is delayed until `type_of(self) == Self` is complete.
return Math.Sqrt(self.x * self.x + self.y * self.y);
}

fn CreatePolarInvalid(r: f32, theta: f32) -> Point {
Expand Down Expand Up @@ -955,7 +955,7 @@ class Point {

fn CreateXAxis(x: f32) -> Point;

fn Angle[me: Self]() -> f32;
fn Angle[self: Self]() -> f32;

var x: f32;
var y: f32;
Expand All @@ -967,10 +967,10 @@ fn Point.CreateXAxis(x: f32) -> Point;
return Create(x, 0);
}

fn Point.Angle[me: Self]() -> f32 {
fn Point.Angle[self: Self]() -> f32 {
// ✅ Allowed: `Point` type is complete.
// Function is checked immediately.
return Math.ATan2(me.y, me.x);
return Math.ATan2(self.y, self.x);
}
```

Expand Down Expand Up @@ -1127,7 +1127,7 @@ declaration before `fn`.

```
base class MyBaseClass {
virtual fn Overridable[me: Self]() -> i32 { return 7; }
virtual fn Overridable[self: Self]() -> i32 { return 7; }
}
```

Expand Down Expand Up @@ -1418,21 +1418,21 @@ the `destructor` keyword:

```carbon
class MyClass {
destructor [me: Self] { ... }
destructor [self: Self] { ... }
}
```

or:

```carbon
class MyClass {
// Can modify `me` in the body.
destructor [addr me: Self*] { ... }
// Can modify `self` in the body.
destructor [addr self: Self*] { ... }
}
```

If a class has no `destructor` declaration, it gets the default destructor,
which is equivalent to `destructor [me: Self] { }`.
which is equivalent to `destructor [self: Self] { }`.

The destructor for a class is run before the destructors of its data members.
The data members are destroyed in reverse order of declaration. Derived classes
Expand All @@ -1450,9 +1450,9 @@ Destructors may be declared in class scope and then defined out-of-line:

```carbon
class MyClass {
destructor [addr me: Self*];
destructor [addr self: Self*];
}
destructor MyClass [addr me: Self*] { ... }
destructor MyClass [addr self: Self*] { ... }
```

It is illegal to delete an instance of a derived class through a pointer to one
Expand All @@ -1464,11 +1464,11 @@ must be `impl`:

```carbon
base class MyBaseClass {
virtual destructor [addr me: Self*] { ... }
virtual destructor [addr self: Self*] { ... }
}

class MyDerivedClass extends MyBaseClass {
impl destructor [addr me: Self*] { ... }
impl destructor [addr self: Self*] { ... }
}
```

Expand Down Expand Up @@ -1518,8 +1518,8 @@ call the `UnsafeDelete` method instead. Note that you may not call
```
interface Allocator {
// ...
fn Delete[T:! Deletable, addr me: Self*](p: T*);
fn UnsafeDelete[T:! Destructible, addr me: Self*](p: T*);
fn Delete[T:! Deletable, addr self: Self*](p: T*);
fn UnsafeDelete[T:! Destructible, addr self: Self*](p: T*);
}
```

Expand Down Expand Up @@ -1566,7 +1566,7 @@ could potentially fail must be performed before the destructor is called.
Unhandled failure during a destructor call will abort the program.

**Future work:** Allow or require destructors to be declared as taking
`[var me: Self]`.
`[var self: Self]`.

**Alternatives considered:**

Expand Down Expand Up @@ -1630,7 +1630,7 @@ As in C++, `private` means only accessible to members of the class and any

```carbon
class Point {
fn Distance[me: Self]() -> f32;
fn Distance[self: Self]() -> f32;
// These are only accessible to members of `Point`.
private var x: f32;
private var y: f32;
Expand Down Expand Up @@ -1668,15 +1668,15 @@ derived classes, and any [friends](#friends).
```
base class MyBaseClass {
protected fn HelperClassFunction(x: i32) -> i32;
protected fn HelperMethod[me: Self](x: i32) -> i32;
protected fn HelperMethod[self: Self](x: i32) -> i32;
protected var data: i32;
}

class MyDerivedClass extends MyBaseClass {
fn UsesProtected[addr me: Self*]() {
fn UsesProtected[addr self: Self*]() {
// Can access protected members in derived class
var x: i32 = HelperClassFunction(3);
me->data = me->HelperMethod(x);
self->data = self->HelperMethod(x);
}
}
```
Expand Down
4 changes: 2 additions & 2 deletions docs/design/expressions/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -232,9 +232,9 @@ with parentheses around the member name:
- _expression_ `.` `(` _expression_ `)`

```
interface I { fn F[me: Self](); }
interface I { fn F[self: Self](); }
class X {}
external impl X as I { fn F[me: Self]() {} }
external impl X as I { fn F[self: Self]() {} }

// `x.I.F()` would mean `(x.I).F()`.
fn Q(x: X) { x.(I.F)(); }
Expand Down
Loading