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

Basic classes: use cases, struct literals, struct types, and future work #561

Merged
merged 83 commits into from
Aug 9, 2021
Merged
Show file tree
Hide file tree
Changes from 80 commits
Commits
Show all changes
83 commits
Select commit Hold shift + click to select a range
f4a7568
Filling out template with PR 561
josh11b Jun 2, 2021
ed189a9
Fill in proposal template
josh11b Jun 3, 2021
f03e0d6
Checkpoint progress.
josh11b Jun 3, 2021
815ee7b
Filling out template with PR 561
josh11b Jun 2, 2021
2c26238
Fill in proposal template
josh11b Jun 3, 2021
5304872
Checkpoint progress.
josh11b Jun 3, 2021
e914354
Update README.
josh11b Jun 3, 2021
cd5b3c2
Merge remote-tracking branch 'refs/remotes/origin/structs-2' into str…
josh11b Jun 3, 2021
7e3933b
Checkpoint progress.
josh11b Jun 3, 2021
4babbcb
Checkpoint progress.
josh11b Jun 4, 2021
5b441c8
Filling out template with PR 561
josh11b Jun 2, 2021
6239f7a
Fill in proposal template
josh11b Jun 3, 2021
12e08df
Checkpoint progress.
josh11b Jun 3, 2021
4895b04
Update README.
josh11b Jun 3, 2021
aa48975
Checkpoint progress.
josh11b Jun 3, 2021
ff68585
Checkpoint progress.
josh11b Jun 4, 2021
c544b0a
Merge branch 'structs-2' of github.com:josh11b/carbon-lang into struc…
josh11b Jun 22, 2021
e9827b3
Iterate on use case text.
josh11b Jun 22, 2021
ba73b43
Checkpoint progress.
josh11b Jun 25, 2021
a5ac27f
Checkpoint progress.
josh11b Jun 25, 2021
25cf0b7
Use cases are in pretty good shape now.
josh11b Jun 25, 2021
7869741
Checkpoint progress.
josh11b Jun 25, 2021
8972a40
First complete version.
josh11b Jun 26, 2021
a703cbd
Checkpoint progress.
josh11b Jun 26, 2021
303fcd5
Merge remote-tracking branch 'upstream/trunk' into structs-2
josh11b Jun 26, 2021
dae391f
Replace "parent/child" with "base/derived type".
josh11b Jun 28, 2021
3a58e7f
Further fix terminology.
josh11b Jun 28, 2021
21a9923
Apply suggestions from code review
josh11b Jun 28, 2021
65b0d39
Use "anonymous" and "nominal" instead of "(un)named"
josh11b Jun 28, 2021
34fd7ba
Better rationale for distinct syntax for types.
josh11b Jun 28, 2021
ca48a17
Address syntax ambiguity of `{`
josh11b Jun 29, 2021
6f94997
Add non-virtual inheritance, TODOs, clarification
josh11b Jun 30, 2021
bd34505
Rename "data type" to "data class"
josh11b Jun 30, 2021
09679f3
Fix formatting and linking.
josh11b Jun 30, 2021
da2d0f8
Difference and overlap between ABC and polymorphic types.
josh11b Jun 30, 2021
13f4f2d
Clarify polymorphic types based on review
josh11b Jun 30, 2021
96bb407
May support ABCs long term
josh11b Jun 30, 2021
2703122
New name for ABCs?
josh11b Jul 1, 2021
b2b2314
Resolution of #494 and `alias`->`let`
josh11b Jul 7, 2021
a7d6fbb
Questions about `let`
josh11b Jul 7, 2021
6e161e9
Part way through redoing use cases
josh11b Jul 14, 2021
4690658
Checkpoint progress.
josh11b Jul 14, 2021
48181d5
Checkpoint progress.
josh11b Jul 15, 2021
2b56c8a
Checkpoint progress.
josh11b Jul 16, 2021
f36ce10
Apply suggestions from code review
josh11b Jul 20, 2021
5cb3318
Apply suggestions from code review
josh11b Jul 20, 2021
8dd821f
Fix line breaks
josh11b Jul 20, 2021
aa0bad5
Incorporate suggestion
josh11b Jul 20, 2021
85cbed7
Big fixes to use cases, add computed properties
josh11b Jul 23, 2021
46ae873
Updates to the last half
josh11b Jul 23, 2021
86941fa
Anonymous type declarations are really just type expressions
josh11b Jul 23, 2021
4be36a1
Implementing interfaces for data classes
josh11b Jul 23, 2021
f0ac344
Expand alternatives considered
josh11b Jul 23, 2021
e4e83d5
Open questions about assign and init
josh11b Jul 24, 2021
2dd640c
Merge remote-tracking branch 'upstream/trunk' into structs-2
josh11b Jul 24, 2021
7cbb93b
Updates
josh11b Jul 24, 2021
201649a
Mixin open questions
josh11b Jul 28, 2021
1fe6d52
Answer an open question
josh11b Jul 28, 2021
d3c6590
Tuples act like data classes for blanket interface impl
josh11b Jul 30, 2021
3e3b6e4
Include resolution of issues #651 and #653
josh11b Jul 30, 2021
7f5abad
`struct` -> `class` in README
josh11b Jul 30, 2021
c856bee
Rename structs.md to classes.md
josh11b Jul 30, 2021
9f7cf2b
struct -> class
josh11b Jul 30, 2021
ccf0f4d
struct -> class in proposal
josh11b Jul 30, 2021
bb3abb8
Clarify that struct types are a subset of data class types
josh11b Jul 30, 2021
0d0aa69
struct -> class in generics design docs
josh11b Jul 30, 2021
2c4e1d5
Fix old `struct` refs, `struct` is not a keyword
josh11b Aug 1, 2021
79794d2
Use for properties from KateGregory
josh11b Aug 3, 2021
f865d76
Apply suggestions from code review
josh11b Aug 3, 2021
2477335
Fix formatting
josh11b Aug 3, 2021
0939ce3
Update problem statement in proposal
josh11b Aug 3, 2021
80eb33c
Match `impl` syntax from #575.
josh11b Aug 3, 2021
0913cbd
"class" instead of "`class`"
josh11b Aug 4, 2021
f66dbfa
Small fixes to "access control" alternatives
josh11b Aug 5, 2021
3543e28
Apply suggestions from code review
josh11b Aug 5, 2021
dc1e48f
Comparisons question is now #710.
josh11b Aug 5, 2021
95c9aa5
Merge branch 'trunk' into structs-2
josh11b Aug 5, 2021
0dc39ea
Fix formatting
josh11b Aug 5, 2021
eae9b5e
Move struct field defaults and option parameters to future work
josh11b Aug 6, 2021
7e04407
Allow trailing comma
josh11b Aug 7, 2021
68720ec
Update docs/design/classes.md
josh11b Aug 7, 2021
ff1c37b
Empty `{}` case, fix formatting
josh11b Aug 7, 2021
85587dc
No `{,}`
josh11b Aug 8, 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
72 changes: 23 additions & 49 deletions docs/design/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
- [Pointers and references](#pointers-and-references)
- [Arrays and slices](#arrays-and-slices)
- [User-defined types](#user-defined-types)
- [Structs](#structs)
- [Classes](#classes)
- [Allocation, construction, and destruction](#allocation-construction-and-destruction)
- [Assignment, copying, and moving](#assignment-copying-and-moving)
- [Comparison](#comparison)
Expand Down Expand Up @@ -170,14 +170,14 @@ cleaned up during evolution.
Name paths in Carbon always start with the package name. Additional namespaces
may be specified as desired.

For example, this code declares a struct `Geometry.Shapes.Flat.Circle` in a
For example, this code declares a class `Geometry.Shapes.Flat.Circle` in a
library `Geometry/OneSide`:

```carbon
package Geometry library("OneSide") namespace Shapes;

namespace Flat;
struct Flat.Circle { ... }
class Flat.Circle { ... }
```

This type can be used from another package:
Expand Down Expand Up @@ -487,7 +487,7 @@ fn Sum(a: Int, b: Int) -> Int {
## Types

> References: [Primitive types](primitive_types.md), [tuples](tuples.md), and
> [structs](structs.md)
> [classes](classes.md)
>
> **TODO:** References need to be evolved.

Expand Down Expand Up @@ -595,19 +595,17 @@ fn RemoveLast(x: (Int, Int, Int)) -> (Int, Int) {

### User-defined types

#### Structs
#### Classes

> References: [Structs](structs.md)
>
> **TODO:** References need to be evolved.
> References: [Classes](classes.md)

`struct`s 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 named product
types.

For example:

```carbon
struct Widget {
class Widget {
var x: Int;
var y: Int;
var z: Int;
Expand All @@ -622,50 +620,26 @@ Breaking apart `Widget`:
- `Widget` has one `String` member: `payload`.
- Given an instance `dial`, a member can be referenced with `dial.paylod`.

More advanced `struct`s may be created:
##### Allocation, construction, and destruction

```carbon
struct AdvancedWidget {
// Do a thing!
fn DoSomething(self: AdvancedWidget, x: Int, y: Int);
> **TODO:** Needs a feature design and a high level summary provided inline.

// A nested type.
struct Nestedtype {
// ...
}
##### Assignment, copying, and moving

private var x: Int;
private var y: Int;
}
You may use a _structural data class literal_, also known as a _struct literal_,
to assign or initialize a variable with a class type.

fn Foo(thing: AdvancedWidget) {
thing.DoSomething(1, 2);
}
```carbon
var sprocket: Widget = {.x = 3, .y = 4, .z = 5, .payload = "Sproing"};
sprocket = {.x = 2, .y = 1, .z = 0, .payload = "Bounce"};
```

Breaking apart `AdvancedWidget`:

- `AdvancedWidget` has a public object method `DoSomething`.
- `DoSomething` explicitly indicates how the `AdvancedWidget` is passed to
it, and there is no automatic scoping - `self` must be specified as the
first input. The `self` name is also a keyword that explains how to
invoke this method on an object.
- `DoSomething` accepts `AdvancedWidget` _by value_, which is easily
expressed here along with other constraints on the object parameter.
- `AdvancedWidget` has two private data members: `x` and `y`.
- Private methods and data members are restricted to use by
`AdvancedWidget` only, providing a layer of easy validation of the most
basic interface constraints.
- `Nestedtype` is a nested type, and can be accessed as
`AdvancedWidget.Nestedtype`.

##### Allocation, construction, and destruction

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

##### Assignment, copying, and moving
You may also copy one struct into another of the same type.

> **TODO:** Needs a feature design and a high level summary provided inline.
```carbon
var thingy: Widget = sprocket;
sprocket = thingy;
```

##### Comparison

Expand Down Expand Up @@ -818,7 +792,7 @@ be used to instantiate the parameterized definition with the provided arguments
in order to produce a complete type. For example:

```carbon
struct Stack(T:$$ Type) {
class Stack(T:$$ Type) {
var storage: Array(T);

fn Push(value: T);
Expand Down
Loading