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

Nominal classes and methods #722

merged 33 commits into from
Aug 23, 2021

Conversation

josh11b
Copy link
Contributor

@josh11b josh11b commented Aug 10, 2021

Add support for nominal (or "named") classes with encapsulation. Inheritance will be in a later proposal. Here is an example of the proposed syntax:

class Circle {
  fn Create(c: Point, r: f32) -> Self {
    return {.center = c, .radius = r};
  }
  fn Diameter[me: Self]() -> f32 {
    return me.radius * 2;
  }
  fn Expand[addr me: Self*](distance: f32);

  private var center: Point;
  private var radius: f32;
}

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

@josh11b josh11b added the proposal A proposal label Aug 10, 2021
@josh11b josh11b requested a review from a team August 10, 2021 17:42
@google-cla google-cla bot added the cla: yes PR meets CLA requirements according to bot. label Aug 10, 2021
@josh11b josh11b marked this pull request as ready for review August 10, 2021 21:25
@josh11b josh11b requested a review from a team as a code owner August 10, 2021 21:25
Copy link
Contributor

@chandlerc chandlerc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Not actually done, just don't want to fail to mash "send". Made it up to "Access control".)

docs/design/README.md Outdated Show resolved Hide resolved
docs/design/classes.md Outdated Show resolved Hide resolved
docs/design/classes.md Outdated Show resolved Hide resolved
docs/design/classes.md Show resolved Hide resolved
docs/design/classes.md Outdated Show resolved Hide resolved
docs/design/classes.md Show resolved Hide resolved
josh11b and others added 2 commits August 10, 2021 20:48
Co-authored-by: Chandler Carruth <chandlerc@gmail.com>
Copy link
Contributor

@chandlerc chandlerc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(ok, now finished!)

docs/design/classes.md Outdated Show resolved Hide resolved
docs/design/classes.md Outdated Show resolved Hide resolved
docs/design/classes.md Show resolved Hide resolved
proposals/p0722.md Outdated Show resolved Hide resolved
Copy link
Contributor

@chandlerc chandlerc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, probably just a TODO to add below.

docs/design/classes.md Show resolved Hide resolved
docs/design/classes.md Show resolved Hide resolved
Copy link
Contributor

@chandlerc chandlerc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just posting a few suggestions that came up in a discussion with @josh11b and @mconst

docs/design/classes.md Outdated Show resolved Hide resolved
docs/design/classes.md Outdated Show resolved Hide resolved
docs/design/classes.md Outdated Show resolved Hide resolved
josh11b and others added 2 commits August 16, 2021 18:17
Co-authored-by: Chandler Carruth <chandlerc@gmail.com>
@chandlerc
Copy link
Contributor

(LGTM and all the thumbs up arrived.)

@josh11b josh11b merged commit 24f9fc7 into carbon-language:trunk Aug 23, 2021
@josh11b josh11b deleted the classes branch August 23, 2021 22:45
@github-actions github-actions bot added the proposal accepted Decision made, proposal accepted label Aug 23, 2021
chandlerc added a commit that referenced this pull request Jun 28, 2022
Add support for nominal (or "named") classes with encapsulation. Inheritance will be in a later proposal. Here is an example of the proposed syntax:

```
class Circle {
  fn Create(c: Point, r: f32) -> Self {
    return {.center = c, .radius = r};
  }
  fn Diameter[me: Self]() -> f32 {
    return me.radius * 2;
  }
  fn Expand[addr me: Self*](distance: f32);

  private var center: Point;
  private var radius: f32;
}

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

Co-authored-by: Chandler Carruth <chandlerc@gmail.com>
@josh11b josh11b mentioned this pull request Jul 23, 2022
josh11b added a commit that referenced this pull request Dec 6, 2022
Carbon has been using a `me: Self` or `addr me: Self*` deduced parameter to mark a function as a method as decided in #494 and implemented in #722 . This does not match existing languages, and so this proposal switches `me` to `self` to match Python, Rust, and Swift.

Co-authored-by: Richard Smith <richard@metafoo.co.uk>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla: yes PR meets CLA requirements according to bot. proposal accepted Decision made, proposal accepted proposal A proposal
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants