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

Nominal classes and methods #722

Merged
merged 33 commits into from
Aug 23, 2021
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
11cb6ea
Filling out template with PR 722
josh11b Aug 10, 2021
5b7edb1
Fill in proposal
josh11b Aug 10, 2021
6260fdc
Update design/README.md
josh11b Aug 10, 2021
52ecf5f
Add new classes content
josh11b Aug 10, 2021
684b74a
Methods and name lookup
josh11b Aug 10, 2021
5eb0e58
Access control
josh11b Aug 10, 2021
87530b1
Checkpoint progress.
josh11b Aug 10, 2021
41a7cf0
Checkpoint progress.
josh11b Aug 10, 2021
0ea7cda
Try using diff code blocks to show allowed/forbidden
josh11b Aug 10, 2021
2b252d4
Revert diff experiment
josh11b Aug 11, 2021
196c13f
Apply suggestions from code review
josh11b Aug 11, 2021
a933ca1
Add assignment, doesn't use field defaults
josh11b Aug 11, 2021
380f25d
Emoji red x -> forbidden code
josh11b Aug 11, 2021
48a042c
Emoji check -> allowed code
josh11b Aug 11, 2021
dbe7317
Split example so one return/method
josh11b Aug 11, 2021
0b1d1a0
Move emoji to comments
josh11b Aug 11, 2021
e47930c
Move method calling alternative
josh11b Aug 11, 2021
1720838
Add Kotlin as example
josh11b Aug 11, 2021
11878b8
No explicit control over linkage
josh11b Aug 11, 2021
4bcb965
"member"/"associated" -> "nested"
josh11b Aug 11, 2021
7ada41e
Rationale for `impl as Data {}`
josh11b Aug 11, 2021
b572336
Class function and member function terminology
josh11b Aug 11, 2021
557f935
Nested type -> member type
josh11b Aug 11, 2021
00cda76
`:!` in let, clarify name lookup
josh11b Aug 11, 2021
01ec40b
Checkpoint progress.
josh11b Aug 11, 2021
dadf910
Link encapsulation
josh11b Aug 12, 2021
0955002
Member types can be choice types
josh11b Aug 12, 2021
dccb54e
Clarify Data interface
josh11b Aug 16, 2021
795cf97
Aliases future work
josh11b Aug 16, 2021
6e251a2
Clarify access needed for assignment
josh11b Aug 16, 2021
bdd7363
Resolve some questions
josh11b Aug 17, 2021
1f92bdf
Apply suggestions from code review
josh11b Aug 17, 2021
c73507a
Fix formatting
josh11b Aug 17, 2021
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
1 change: 1 addition & 0 deletions .codespell_ignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

circularly
copyable
crate
inout
pullrequest
statics
75 changes: 67 additions & 8 deletions docs/design/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,11 @@ SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
- [Arrays and slices](#arrays-and-slices)
- [User-defined types](#user-defined-types)
- [Classes](#classes)
- [Assignment, copying](#assignment-copying)
- [Member access](#member-access)
- [Methods](#methods)
- [Allocation, construction, and destruction](#allocation-construction-and-destruction)
- [Assignment, copying, and moving](#assignment-copying-and-moving)
- [Moving](#moving)
- [Comparison](#comparison)
- [Implicit and explicit conversion](#implicit-and-explicit-conversion)
- [Inline type composition](#inline-type-composition)
Expand Down Expand Up @@ -599,8 +602,7 @@ fn RemoveLast(x: (Int, Int, Int)) -> (Int, Int) {

> References: [Classes](classes.md)

Classes are a way for users to define their own data strutures or named product
types.
Classes are a way for users to define their own data strutures or record types.

For example:

Expand All @@ -620,11 +622,7 @@ Breaking apart `Widget`:
- `Widget` has one `String` member: `payload`.
- Given an instance `dial`, a member can be referenced with `dial.paylod`.

##### Allocation, construction, and destruction

> **TODO:** Needs a feature design and a high level summary provided inline.

##### Assignment, copying, and moving
##### Assignment, copying

You may use a _structural data class literal_, also known as a _struct literal_,
to assign or initialize a variable with a class type.
Expand All @@ -641,6 +639,67 @@ var thingy: Widget = sprocket;
sprocket = thingy;
```

##### Member access

The data members of a variable with a class type may be accessed using dot `.`
notation:

```carbon
Assert(sprocket.x == thingy.x);
```

##### Methods

Class type definitions can include methods:

```carbon
class Point {
fn Distance[me: Self](x2: i32, y2: i32) -> f32 {
var dx: i32 = x2 - me.x;
var dy: i32 = y2 - me.y;
return Math.Sqrt(dx * dx - dy * dy);
}
fn Offset[addr me: Self*](dx: i32, dy: i32);

var x: i32;
var y: i32;
}

fn Point.Offset[addr me: Self*](dx: i32, dy: i32) {
me->x += dx;
me->y += dy;
}

var origin: Point = {.x = 0, .y = 0};
Assert(Math.Abs(origin.Distance(3, 4) - 5.0) < 0.001);
origin.Offset(3, 4);
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 functions with a `me` 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
declaration.
- `origin.Offset(...)` does modify the value of `origin`. This is signified
josh11b marked this conversation as resolved.
Show resolved Hide resolved
using `[addr me: Self*]` in the method declaration.
- Methods may be declared lexically inline like `Distance`, or lexically out
of line like `Offset`.

##### Allocation, construction, and destruction

> **TODO:** Needs a feature design and a high level summary provided inline.

##### Moving

> **TODO:** Needs a feature design and a high level summary provided inline.

##### Comparison

> **TODO:** Needs a feature design and a high level summary provided inline.
Expand Down
Loading