Commit Graph
157 Commits
Author SHA1 Message Date
josh11bandRichard Smith f4e9063b97 Generics details 8: interface default and final members (#990)
Allowing interfaces to define default values for its associated entities. This:

-   Helps with evolution by reducing the changes needed to add new members to an interface.
-   Reduces boilerplate when some value is more common than others.
-   Addresses the gap between the minimum necessary for a type to provide the desired functionality of an interface and the breadth of API that user's desire.

As an alternative, final values can be provided instead, which can't be overridden, but are more predictable for users and may avoid dynamic dispatch overhead in some cases.

Example:
```
// Interface parameter has a default of `Self`
interface Add(Right:! Type = Self) {
  // `AddWith` *always* equals `Right`
  final let AddWith:! Type = Right;
  // `Result` has a default of `Self`
  let Result:! Type = Self;
  fn DoAdd[me: Self](right: Right) -> Result;
}

impl String as Add() {
  // Right == AddWith == Result == Self == String
  fn DoAdd[me: Self](right: Self) -> Self;
}
```

Co-authored-by: Richard Smith <richard@metafoo.co.uk>
2022-01-21 14:10:20 -08:00
Jon Meow adee1d7c94 Remove operators.md in favor of expressions/ (#1031) 2022-01-18 15:13:36 -08:00
josh11b ad5072b7b8 Link to Swift re: origin of witness table term (#1029) 2022-01-18 11:17:09 -08:00
josh11bandRichard Smith c08cbdb0bd Clarify class declaration syntax (#1026)
Clarify class declaration syntax

Co-authored-by: Richard Smith <richard@metafoo.co.uk>
2022-01-14 14:23:43 -08:00
josh11bandRichard Smith 00a178769f Principle: One static open extension mechanism (#998)
This is a proposal to adopt interfaces as the single static open extension mechanism for things like selecting how operators are implemented for types.

Co-authored-by: Richard Smith <richard@metafoo.co.uk>
2022-01-07 15:54:27 -08:00
Jon Meow b7df523dc8 Fix cross-file links in non-proposal files (#1010) 2022-01-07 11:00:21 -08:00
b333f48697 Generics details 6: remove facets (#950)
There were some concerns about facet types leaking out of generic code in return types. Some initial fixes for this were done in [PR #900](https://github.com/carbon-language/carbon-lang/pull/900), but there remain concerns, for example when associated types are involved.

In particular, given an interface method with return type using an associated type, as in:

```
interface Deref {
  let Result:! Type;
  fn DoDeref[me: Self]() -> Result;
}

class IntHandle {
  impl as Deref {
    let Result:! Type = i32;
    fn DoDeref[me: Self]() -> Result { ... }
  }
}
```

Since `Result` has type `Type`, we had the problem that `IntHandle.DoDeref` would have to return `i32 as Type`, instead of the desired `i32`.

We also think we can simplify the model by eliminating the facet type concept and syntax.

This proposal removes facet types, introduces archetypes in their place, clarifies how associated types work outside of a generic function, and specifies how a generic `let` statement in a function body works.

Co-authored-by: Wolff Dobson <wolffg@users.noreply.github.com>
Co-authored-by: Richard Smith <richard@metafoo.co.uk>
2022-01-06 14:08:08 -08:00
josh11bandRichard Smith 1534970146 Implicit conversions for aggregates (#981)
Define initialization, assignment, comparison, and implicit conversion between data classes with different field orders and/or types.

Implements the decision made in #710 .

Co-authored-by: Richard Smith <richard@metafoo.co.uk>
2022-01-06 11:16:41 -08:00
Jon Meowandjosh11b 24b763c7e8 Fix or remove invalid anchor links, adding pre-commit (#997)
Co-authored-by: josh11b <josh11b@users.noreply.github.com>
2022-01-05 15:00:02 -08:00
josh11bandChandler Carruth 3f12316d6b Generics details 7: final impls (#983)
Add support for marking impls as `final` to say they can't be specialized. This allows generic functions that see that the impl applies to determine the values for its associated types. For example this allows us to say that the implementation of the `Deref` interface for pointers can't be specialized. Otherwise, `*p` could have unknown type in a generic function.

Co-authored-by: Chandler Carruth <chandlerc@gmail.com>
2022-01-04 14:17:41 -08:00
josh11b 3cb3ee32dd Double -> f64 (#991)
Replace `Double` with `f64` for the 64-bit floating point type.
2021-12-15 13:33:47 -08:00
josh11bandRichard Smith dcc287cb6e Generic parameterized impls (details 5) (#920)
There are cases where an impl definition should apply to more than a single type and interface combination. The solution is to parameterize the impl definition, so it applies to a family of types, interfaces, or both. This includes:

-   Declare an impl for a parameterized type, which may be external or declared out-of-line.
    ```
    external impl [T:! Type] Vector(T) as Iterable { ... }
    external impl Vector(T:! Type) as Iterable { ... }
    ```
-   "Conditional conformance" where a parameterized type implements some interface if the parameter to the type satisfies some criteria, like implementing the same interface.
    ```
    external impl [T:! Type] Pair(T, T) as Foo(T) { ... }
    class Array(T:! Type, template N:! Int) {
      impl [P:! Printable] Array(P, N) as Printable { ... }
      impl Array(P:! Printable, N) as Printable { ... }
    }
    ```
-   "Blanket" impls where an interface is implemented for all types that implement another interface, or some other criteria beyond being a specific type.
    ```
    external impl [T:! Ordered] T as PartiallyOrdered { ... }
    ```
-   "Wildcard" impls where a family of interfaces are implemented for single type.
    ```
    class BigInt {
      external impl [T:! ImplicitAs(i32)] as AddTo(T) { ... }
      external impl as AddTo(T:! ImplicitAs(i32)) { ... }
    }
    external impl [T:! ImplicitAs(i32)] BigInt as AddTo(T) { ... }
    external impl BigInt as AddTo(T:! ImplicitAs(i32)) { ... }
    ```

In addition to a syntax for defining parameterized impls, we need rules for coherence:

-   Orphan rules that ensure that impls are imported in any code that might use it.
-   We need overlap rules that pick a specific impl when more than one impl declaration matches a specific query about whether a type implements an interface.

Co-authored-by: Richard Smith <richard@metafoo.co.uk>
2021-12-09 14:29:54 -08:00
634148c39f Coherence: terminology, rationale, alternatives considered (#624)
Add an appendix with the rationale for and alternatives to coherence, along with an entry in the terminology and updates to the coherence discussion in the goals.

Co-authored-by: Chandler Carruth <chandlerc@gmail.com>
Co-authored-by: Richard Smith <richard@metafoo.co.uk>
2021-12-09 13:27:15 -08:00
josh11bandRichard Smith be8d0a993b Generic impls access (details 4) (#931)
Implementations of interfaces are as public as the names used in their signature. No access control modifiers are allowed on `impl` declarations.

Co-authored-by: Richard Smith <richard@metafoo.co.uk>
2021-12-07 09:28:08 -08:00
josh11bandRichard Smith 3f759ed58c Update and edit pass through generic design docs (#965)
Includes a variety of changes:

Int -> i32
this -> me
expand terminology doc and add links to it
fix text to reflect inline external impl introduced in Support external impl in class and adapter scopes. #905
no longer have plans for runtime type parameters
style updates like removing parentheticals and "we"
observe is a "declaration" not a "statement", since it can appear outside function bodies
many individual updates, clean-ups, and fixes

Co-authored-by: Richard Smith <richard@metafoo.co.uk>
2021-12-07 09:17:23 -08:00
89a829b8c7 Constraints for generics (generics details 3) (#818)
This proposal describes `where` clauses that can add constraints on a type-of-type, for example define restrictions on its associated types. Example:

```
fn FindFirstPrime[T:! Container where .Element = i32]
    (c: T) -> Optional(i32) {
  // The elements of `c` have type `T.Element`, which is `i32`.
  ...
}

fn PrintContainer[T:! Container where .Element is Printable](c: T) {
  // The type of the elements of `c` is not known, but we do know
  // that type satisfies the `Printable` interface.
  ...
}
```

Some other constraints, such as `Sized` are defined as type-of-types directly, possibly parameterized.

Co-authored-by: Richard Smith <richard@metafoo.co.uk>
Co-authored-by: Chandler Carruth <chandlerc@gmail.com>
2021-10-29 20:32:22 -07:00
05efb278c2 as expressions (#845)
This proposal provides an `as` expression for casting. This supports implicit conversions plus some safe and unsurprising conversions that we do not support implicitly:

* lossy but fully defined conversions to floating-point types
* conversion from `bool` to integer types
* conversion between adaptors and their adapted type, and more generally between compatible types

This facility can be extended by implementing the `As(TargetType)` interface for a type.

Co-authored-by: Geoff Romer <gromer@google.com>
Co-authored-by: josh11b <josh11b@users.noreply.github.com>
2021-10-29 16:46:19 -07:00
josh11b b08e3fedeb Support external impl in class and adapter scopes. (#905) 2021-10-26 09:25:30 -07:00
josh11b 8d051e2b6b Update constructor syntax (#913) 2021-10-25 13:05:39 -07:00
josh11b 629cfb8c4d Indent code blocks in bulletted list (#901) 2021-10-19 15:57:42 -07:00
josh11b dc9c36660b Facet relationship between parameterized types (#900) 2021-10-19 13:57:29 -07:00
josh11b cfb36b85e5 Merge overview sections (#899) 2021-10-19 11:58:48 -07:00
Jon Meow 9c17b72ddd Clean up titles for a few docs, mainly to remove 'Carbon' prefixes (#894)
Note, most principle docs already have the `Principle:` prefix, context sensitivity and `Safety strategy` are/were outliers.
2021-10-15 15:03:36 -07:00
josh11bandRichard Smith c7c653adba Generics: cleanups and updates (#881)
Add references, update syntax, merge future work

Co-authored-by: Richard Smith <richard@metafoo.co.uk>
2021-10-15 12:52:12 -07:00
Richard Smith 93a002a21a Avoid confusing description of "equivalent". (#843) 2021-10-06 11:06:34 -07:00
Richard Smith aec77a6f39 Add a keyword list. (#869)
This list was extracted from currently-approved proposals and
corresponding design documents. This is intended to be a summary of the
status quo, not a change.
2021-10-06 11:05:41 -07:00
Richard Smith 0539931b76 PR 866: Allow ties in floating literals. (#866) 2021-10-02 11:01:29 -07:00
Geoff RomerandChandler Carruth bf49f2efed Proposal: Property naming in C++ (#720)
This proposed style change allows C++ classes in the Carbon project to provide methods that are named like variables, so long as they behave like _properties_ of the class. It also requires data member names to have a trailing `_`.

Co-authored-by: Chandler Carruth <chandlerc@gmail.com>
2021-09-28 14:36:43 -07:00
Jon Meow 563201a849 Remove proposals/BUILD, moving template.md under scripts (#846)
With #842 there's not as much reason for the BUILD anymore, maybe move the template.md file and get rid of it?
2021-09-23 09:16:19 -07:00
f63169608e Implicit conversions (#820)
Proposal to support a limited set of implicit conversions.

This would generally permit only implicit conversions that are lossless and semantics-preserving. In particular, this proposal allows:

-   Conversion from an integer type to a wider integer type of the same signedness, and from an unsigned integer type to a wider signed integer type.
-   Conversion from an integer type to a floating-point type that has enough mantissa bits to exactly represent all integers in the source type.
-   Conversion from integer literals to integer and floating-point types that can represent them.
-   Conversion from floating-point literals to floating-point types that can represent them.
-   Conversions required for generics: conversions of values between facet types, and conversions of types between type-of-types, as described in the generics proposals.
-   Conversions required for inheritance: derived-to-base conversions for class pointers and class values.

Other conversions, such as lossy conversions between arithmetic types and conversions between bool and other types are not supported.

Co-authored-by: josh11b <josh11b@users.noreply.github.com>
Co-authored-by: Chandler Carruth <chandlerc@gmail.com>
Co-authored-by: Geoff Romer <gromer@google.com>
2021-09-21 15:16:16 -07:00
Jon MeowandChandler Carruth c3f4f28ec5 One way principle (#829)
Add a principle that Carbon should only provide one way of doing things

Co-authored-by: Chandler Carruth <chandlerc@gmail.com>
2021-09-21 09:45:35 -07:00
Jon MeowandChandler Carruth 6d33a80988 api file default public (#752)
Replace library public-like `api` behavior with explicit `private` behavior, mirroring #665 

Co-authored-by: Chandler Carruth <chandlerc@gmail.com>
2021-09-21 09:33:32 -07:00
Richard Smith 0fec34df03 Tiny doc update: var is not an expression. (#812) 2021-09-14 13:17:31 -07:00
3610325c38 Inheritance (#777)
This proposal adds inheritance to classes, following this syntax:
```
// Abstract classes may not be instantiated, but
// may have abstract methods and be extended.
abstract class AbstractBaseClass {
  virtual fn CanBeOverridden[me: Self]() -> i32 {
    return me.data;
  }
  abstract fn PureVirtual[me: Self]() -> i32;
  fn Create(data: i32) -> partial Self {
    return {.data = data};
  }
  protected var data: i32;
}

// Classes are final by default
class FinalClass extends AbstractBaseClass {
  impl fn PureVirtual[me: Self]() -> i32 {
    return me.x;
  }
  fn Create(data: i32) -> Self {
    return {.base = AbstractBaseClass.Create(data), .x = 2 * data};
  }
  private var x: i32;
}

// Can be instantiated and extended
base class ExtensibleClass {
  // May optionally use partial types in factory functions
  protected fn CreateAsBase(data: i32) -> partial Self { ... }
  fn Create(data: i32) -> Self { ... }
}
```

Co-authored-by: Chandler Carruth <chandlerc@gmail.com>
Co-authored-by: Richard Smith <richard@metafoo.co.uk>
2021-09-08 17:37:35 -07:00
225eda7f49 Generics details 2: adapters, associated types, parameterized interfaces (#731)
This proposal goes into the details for these features of generics:

- adapters: for creating new types compatible with existing types but with different interface implementations
- associated types: allowing an interface implementation to specify some types to use in method signatures
- interface parameters: creating a family of interfaces, where types can implement more than one

This is a continuation of #553 . It has been summarized in these presentations:

- adapters: [1](https://docs.google.com/presentation/d/1bg6q0Q9Sk4YpRbNA3D3H34xYtaEO8ScAUNUZK2UTi80/edit?resourcekey=0-6-Y6e1mfRUmHg-Zk65Gc5A#slide=id.gcf40df1c7b_0_37) and [2](https://docs.google.com/presentation/d/17KG0TeJ4OChMRdLJPS8TE_K6SoL4lFy1FUGr2CDzX-A/edit?resourcekey=0-kLnZqd5NrbGSwmbunTyB-A#slide=id.g7a37009490_0_0)
- [associated types and interface parameters](https://docs.google.com/presentation/d/19hPpUjxQ0H1lUSLy5QjS2910Cpc7UdNKpF580fFsCGw/edit?resourcekey=0-ky9XGRC1I8X0Ffw6eqh7WQ#slide=id.p)

Co-authored-by: Wolff Dobson <wolffg@users.noreply.github.com>
Co-authored-by: Richard Smith <richard@metafoo.co.uk>
2021-09-05 15:50:53 -07:00
Jon Meow eb61412c79 Explicitly disallow self-imports by libraries (#794) 2021-09-03 17:01:52 -07:00
Geoff Romer e08beccc66 Use "package directive" and "import directive" (#808)
These aren't really statements (because they're not executed at run time), and it's debatable whether they're declarations, since their primary purpose isn't to introduce a name. On Discord, "directive" seemed to be the consensus choice for an alternate term.
2021-09-03 11:54:41 -07:00
Jon Meow 49013ae1cc Reject invalid string literal whitespace on unescape (#793)
This is based on discussion on #732: that we should probably parse the invalid whitespace, then reject it as part of string validation, rather than having different parses. I worry the question of "how is this parsed" may lead to subtly unexpected results if we aren't consistent, so I'm switching the logic from the lexer to the unescape library (and also adjusting the list of rejected whitespace).
2021-08-30 15:22:03 -07:00
Jon MeowandRichard Smith 08c538af86 Update alternatives/references style in lexical conventions (#727)
Co-authored-by: Richard Smith <richard@metafoo.co.uk>
2021-08-27 15:47:02 -07:00
Jon MeowandRichard Smith 30682750ce Updated string literal design based on #199 (#732)
I've modified the text from the proposal slightly, focusing the overview more on a design setup, but mostly kept the details. One important thing here is I noticed that raw tab characters are disallowed -- this was a little buried before, and I've now updated the list of characters allowed in a string to exclude tabs. Additionally, I've noted `\0D` in the list of escapes as explicitly invalid.

Co-authored-by: Richard Smith <richard@metafoo.co.uk>
2021-08-27 15:43:14 -07:00
Chandler Carruth 821daa220a Adjust the evolution process based on experience. (#764)
Recent experience indicates that the system of thumbs-up doesn't seem to
work consistently. Replace it with more broad encouragement to use PR
reactions to surface sentiment and a judgement call by the leads on when
a proposal is ready to merge.

An explicit goal here is that leads can make this judgement call
reflecting the nature of the proposal. Many of these are low-risk.
Either they are easily fixed-forward or minimally disruptive. This can
be because they are merely beginning to fill out a largely open area, or
because they are minor changes.

Also try to clarify that it is expected for the leads to sometimes miss
things or make mistakes, and encourage a revert or fix-forward mentality
rather than slowing down progress to reduce the rate of mistakes.
2021-08-27 15:31:31 -07:00
Jon Meow d550256c93 Delete the website and references (#782)
.pre-commit-config.yaml and proposals/p0540.md (linking goals.html, fixed to goals.md link) are the two references I could find.

The site is disabled, though; I think it makes the most sense to delete supporting code, and in the future hopefully we can use GH pages to publish.
2021-08-25 14:15:01 -07:00
Jon MeowandChandler Carruth 5042d96863 Switch to a brew version of python3.9 (#778)
- brew instead of pyenv to centralize package management
  - can't recall why we used pyenv before, may not have been the best choice.
  - pyenv has been a burden in updating versions, a single `brew upgrade` works better
- python3.9 because it's the latest and greatest, feels weird going to old versions if we're recommending installs.
- bazel 5.0.0 pre-release due to https://github.com/bazelbuild/bazel/commit/b9fc66d327debcfbdb2964afdba35a1cc8919b81
  - earlier versions want `python` to be on the path, which pyenv did but brew does not

For most people this will mean: `rm -rf ~/.pyenv && brew uninstall pyenv && brew install python@3.9 && pip3 install -U pip && pip3 install pre-commit`, plus removing any `pyenv` invocations from the shell `rc` file.

Co-authored-by: Chandler Carruth <chandlerc@gmail.com>
2021-08-25 09:10:35 -07:00
josh11bandChandler Carruth 0820dec01f Nominal classes and methods (#722)
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>
2021-08-23 15:45:41 -07:00
Jon Meow b73ad7dd1a Update references/alternatives in return doc (#730) 2021-08-17 08:35:02 -07:00
Jon Meow aebfa7dac9 Update alternatives/references style in variables (#728)
Moving "Global variables" out of "Alternatives"; I don't think it's really been considered in the same way that others have, so there's no reference to provide.
2021-08-12 08:31:35 -07:00
Jon MeowandRichard Smith 8d3d1b1233 Flesh out loops docs a little (#729)
Co-authored-by: Richard Smith <richard@metafoo.co.uk>
2021-08-12 08:20:46 -07:00
Jon Meow 6c5cf38879 Rewrite source file alternatives/references (#723) 2021-08-12 08:19:24 -07:00
Jon Meow c33994d1d1 Rewrite code and name organization alternatives/references (#724)
Alternatives just link to the proposal PR due to the legacy decision of putting alternatives in the doc.
2021-08-12 08:18:39 -07:00
josh11bandRichard Smith ecb5a611e5 Generics details part 1 (#553)
This proposal goes into the details of the core of the generics feature, to achieve the goals from #24 , and provides an outline covering future work. It has been summarized in these presentations:

- [basic usage](https://docs.google.com/presentation/d/1OZiMTVW2Ommop5WTs9RyEwnGxy9yzaAPF7Cj5KUfDsY/edit?resourcekey=0-Nya0Soz3ZNs3hJan8VIrTA#slide=id.p)
- [details: interfaces](https://docs.google.com/presentation/d/1FSlqtE5hXZIwOO52UrAK9DINBLDWtgu24dugHfomUMg/edit#slide=id.p)
- [details: facet types](https://docs.google.com/presentation/d/17KG0TeJ4OChMRdLJPS8TE_K6SoL4lFy1FUGr2CDzX-A/edit?resourcekey=0-kLnZqd5NrbGSwmbunTyB-A#slide=id.p)
- [details: type-types](https://docs.google.com/presentation/d/1Hn3VDlVjwhjx3SKM2KXKE7lW208nXff30x3-uIO4_Fo/edit#slide=id.p)
- [details: extending/refining interfaces](https://docs.google.com/presentation/d/1K0cCHeb9JTJY9QCGEVO9CcJNHYlaXkoPESv4J9tl5LU/edit#slide=id.p)

Co-authored-by: Richard Smith <richard@metafoo.co.uk>
2021-08-10 20:46:23 -07:00