Skip to content

Commit

Permalink
Remove : in variable declarations (#503)
Browse files Browse the repository at this point in the history
Starting to apply #339

Co-authored-by: Richard Smith <[email protected]>
  • Loading branch information
jonmeow and zygoloid authored May 17, 2021
1 parent 807f934 commit 3149198
Show file tree
Hide file tree
Showing 12 changed files with 77 additions and 78 deletions.
62 changes: 31 additions & 31 deletions docs/design/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ package ExampleUser;
import Geometry library("OneSide");
fn Foo(var Geometry.Shapes.Flat.Circle: circle) { ... }
fn Foo(Geometry.Shapes.Flat.Circle circle) { ... }
```

### Names and scopes
Expand Down Expand Up @@ -282,7 +282,7 @@ Some common expressions in Carbon include:
Functions are the core unit of behavior. For example:

```carbon
fn Sum(Int: a, Int: b) -> Int;
fn Sum(Int a, Int b) -> Int;
```

Breaking this apart:
Expand Down Expand Up @@ -333,7 +333,7 @@ For example:

```carbon
fn Foo() {
var Int: x = 42;
var Int x = 42;
}
```

Expand Down Expand Up @@ -371,7 +371,7 @@ conditional execution of statements.
For example:

```carbon
fn Foo(Int: x) {
fn Foo(Int x) {
if (x < 42) {
Bar();
} else if (x > 77) {
Expand Down Expand Up @@ -400,7 +400,7 @@ For example:

```carbon
fn Foo() {
var Int: x = 0;
var Int x = 0;
while (x < 42) {
if (ShouldStop()) break;
if (ShouldSkip(x)) {
Expand Down Expand Up @@ -435,7 +435,7 @@ value is provided by an expression in the return statement. This allows us to
complete the definition of our `Sum` function from earlier as:

```carbon
fn Sum(Int: a, Int: b) -> Int {
fn Sum(Int a, Int b) -> Int {
return a + b;
}
```
Expand Down Expand Up @@ -502,7 +502,7 @@ tuple. In formal type theory, tuples are product types.
An example use of tuples is:

```carbon
fn DoubleBoth(Int: x, Int: y) -> (Int, Int) {
fn DoubleBoth(Int x, Int y) -> (Int, Int) {
return (2 * x, 2 * y);
}
```
Expand All @@ -519,7 +519,7 @@ expression: one is a tuple of types, the other a tuple of values.
Element access uses subscript syntax:

```carbon
fn DoubleTuple((Int, Int): x) -> (Int, Int) {
fn DoubleTuple((Int, Int) x) -> (Int, Int) {
return (2 * x[0], 2 * x[1]);
}
```
Expand All @@ -528,12 +528,12 @@ Tuples also support multiple indices and slicing to restructure tuple elements:

```carbon
// This reverses the tuple using multiple indices.
fn Reverse((Int, Int, Int): x) -> (Int, Int, Int) {
fn Reverse((Int, Int, Int) x) -> (Int, Int, Int) {
return x[2, 1, 0];
}
// This slices the tuple by extracting elements [0, 2).
fn RemoveLast((Int, Int, Int): x) -> (Int, Int) {
fn RemoveLast((Int, Int, Int) x) -> (Int, Int) {
return x[0 .. 2];
}
```
Expand Down Expand Up @@ -565,11 +565,11 @@ For example:

```carbon
struct Widget {
var Int: x;
var Int: y;
var Int: z;
var Int x;
var Int y;
var Int z;
var String: payload;
var String payload;
}
```

Expand All @@ -584,18 +584,18 @@ More advanced `struct`s may be created:
```carbon
struct AdvancedWidget {
// Do a thing!
fn DoSomething(AdvancedWidget: self, Int: x, Int: y);
fn DoSomething(AdvancedWidget self, Int x, Int y);
// A nested type.
struct Nestedtype {
// ...
}
private var Int: x;
private var Int: y;
private var Int x;
private var Int y;
}
fn Foo(AdvancedWidget: thing) {
fn Foo(AdvancedWidget thing) {
thing.DoSomething(1, 2);
}
```
Expand Down Expand Up @@ -667,13 +667,13 @@ fn Bar() -> (Int, (Float, Float));
fn Foo() -> Float {
match (Bar()...) {
case (42, (Float: x, Float: y)) => {
case (42, (Float x, Float y)) => {
return x - y;
}
case (Int: p, (Float: x, Float: _)) if (p < 13) => {
case (Int p, (Float x, Float _)) if (p < 13) => {
return p * x;
}
case (Int: p, auto: _) if (p > 3) => {
case (Int p, auto _) if (p > 3) => {
return p * Pi;
}
default => {
Expand All @@ -690,7 +690,7 @@ Breaking apart this `match`:
- It then will find the _first_ `case` that matches this value, and
execute that block.
- If none match, then it executes the default block.
- Each `case` pattern contains a value pattern, such as `(Int: p, auto: _)`,
- Each `case` pattern contains a value pattern, such as `(Int p, auto _)`,
followed by an optional boolean predicate introduced by the `if` keyword.
- The value pattern must first match, and then the predicate must also
evaluate to true for the overall `case` pattern to match.
Expand All @@ -704,7 +704,7 @@ Value patterns may be composed of the following:
- The special identifier `_` may be used to discard the value once
matched.
- A destructuring pattern containing a sequence of value patterns, such as
`(Float: x, Float: y)`, which match against tuples and tuple-like values by
`(Float x, Float y)`, which match against tuples and tuple-like values by
recursively matching on their elements.
- An unwrapping pattern containing a nested value pattern which matches
against a variant or variant-like value by unwrapping it.
Expand All @@ -724,7 +724,7 @@ An example use is:
```carbon
fn Bar() -> (Int, (Float, Float));
fn Foo() -> Int {
var (Int: p, auto: _) = Bar();
var (Int p, auto _) = Bar();
return p;
}
```
Expand All @@ -733,7 +733,7 @@ To break this apart:

- The `Int` returned by `Bar()` matches and is bound to `p`, then returned.
- The `(Float, Float)` returned by `Bar()` matches and is discarded by
`auto: _`.
`auto _`.

### Pattern matching as function overload resolution

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

```carbon
struct Stack(Type:$$ T) {
var Array(T): storage;
struct Stack(Type$$ T) {
var Array(T) storage;
fn Push(T: value);
fn Push(T value);
fn Pop() -> T;
}
```
Expand All @@ -805,12 +805,12 @@ arguments. The runtime call then passes the remaining arguments to the resulting
complete definition.

```carbon
fn Convert[Type:$$ T](T: source, Type:$$ U) -> U {
var U: converted = source;
fn Convert[Type$$ T](T source, Type$$ U) -> U {
var U converted = source;
return converted;
}
fn Foo(Int: i) -> Float {
fn Foo(Int i) -> Float {
// Instantiates with the `T` implicit argument set to `Int` and the `U`
// explicit argument set to `Float`, then calls with the runtime value `i`.
return Convert(i, Float);
Expand Down
14 changes: 7 additions & 7 deletions docs/design/code_and_name_organization/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ package Checksums library "Sha" api;
namespaces Sha256;
api fn Sha256.HexDigest(Bytes: data) -> String { ... }
api fn Sha256.HexDigest(Bytes data) -> String { ... }
```

Calling code may look like:
Expand All @@ -444,9 +444,9 @@ package Caller api;
import Checksums library "Sha";
fn Process(Bytes: data) {
fn Process(Bytes data) {
...
var String: digest = Checksums.Sha256.HexDigest(data);
var String digest = Checksums.Sha256.HexDigest(data);
...
}
```
Expand Down Expand Up @@ -514,7 +514,7 @@ package Geometry api;
import Geometry library "Shapes";
// Circle must be referenced using the Geometry namespace of the import.
fn GetArea(Geometry.Circle: c) { ... }
fn GetArea(Geometry.Circle c) { ... }
```

### Namespaces
Expand Down Expand Up @@ -801,7 +801,7 @@ syntax. For example:
```carbon
import Cpp file("myproject/myclass.h");
fn MyCarbonCall(var Cpp.MyProject.MyClass: x);
fn MyCarbonCall(Cpp.MyProject.MyClass x);
```

### Imports from URLs
Expand Down Expand Up @@ -869,7 +869,7 @@ struct Quantiles {
fn Stats();
fn Build() {
...
var Math.Stats: b;
var Math.Stats b;
...
}
}
Expand Down Expand Up @@ -1790,7 +1790,7 @@ example:
import Geometry library "Shapes" names *;
// Triangle was imported as part of "*".
fn Draw(var Triangle: x) { ... }
fn Draw(Triangle x) { ... }
```

Advantages:
Expand Down
4 changes: 2 additions & 2 deletions docs/design/control_flow.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ control flow constructs are mostly similar to those in C, C++, and other
languages.

```
fn Foo(Int: x) {
fn Foo(Int x) {
if (x < 42) {
Bar();
} else if (x > 77) {
Expand All @@ -60,7 +60,7 @@ an expression in the return statement. This allows us to complete the definition
of our `Sum` function from earlier as:

```
fn Sum(Int: a, Int: b) -> Int {
fn Sum(Int a, Int b) -> Int {
return a + b;
}
```
Expand Down
4 changes: 2 additions & 2 deletions docs/design/functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ primarily divided up into "functions" (or "procedures", "subroutines", or
language. Let's look at a simple example to understand how these work:

```
fn Sum(Int: a, Int: b) -> Int;
fn Sum(Int a, Int b) -> Int;
```

This declares a function called `Sum` which accepts two `Int` parameters, the
Expand All @@ -47,7 +47,7 @@ auto Sum(std::int64_t a, std::int64_t b) -> std::int64_t;
Let's look at how some specific parts of this work. The function declaration is
introduced with a keyword `fn` followed by the name of the function `Sum`. This
declares that name in the surrounding scope and opens up a new scope for this
function. We declare the first parameter as `Int: a`. The `Int` part is an
function. We declare the first parameter as `Int a`. The `Int` part is an
expression (here referring to a constant) that computes the type of the
parameter. The `:` marks the end of the type expression and introduces the
identifier for the parameter, `a`. The parameter names are introduced into the
Expand Down
6 changes: 3 additions & 3 deletions docs/design/lexical_conventions/numeric_literals.md
Original file line number Diff line number Diff line change
Expand Up @@ -440,8 +440,8 @@ Disadvantages:
Advantages:

- Simpler, more flexible rule, that may allow some groupings that are
conventional in a specific domain. For example, `var Date: d = 01_12_1983;`,
or `var Int64: time_in_microseconds = 123456_000000;`.
conventional in a specific domain. For example, `var Date d = 01_12_1983;`,
or `var Int64 time_in_microseconds = 123456_000000;`.
- Culturally agnostic. For example, the Indian convention for digit separators
would group the last three digits, and then every two digits before that
(1,23,45,678 could be written `1_23_45_678`).
Expand All @@ -466,7 +466,7 @@ Disadvantages:
be desirable. For example:

```carbon
var Float32: flt_max =
var Float32 flt_max =
BitCast(Float32, 0b0_11111110_11111111111111111111111);
```
Expand Down
2 changes: 1 addition & 1 deletion docs/design/name_lookup.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ namespace Foo {
}
}
fn F(Foo.Bar.MyInt: x);
fn F(Foo.Bar.MyInt x);
```

Carbon packages are also namespaces so to get to an imported name from the
Expand Down
15 changes: 7 additions & 8 deletions docs/design/pattern_matching.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ under active investigation for C++. Carbon's `match` can be used as follows:
fn Bar() -> (Int, (Float, Float));
fn Foo() -> Float {
match (Bar()) {
case (42, (Float: x, Float: y)) => {
case (42, (Float x, Float y)) => {
return x - y;
}
case (Int: p, (Float: x, Float: _)) if (p < 13) => {
case (Int p, (Float x, Float _)) if (p < 13) => {
return p * x;
}
case (Int: p, auto: _) if (p > 3) => {
case (Int p, auto _) if (p > 3) => {
return p * Pi;
}
default => {
Expand All @@ -70,7 +70,7 @@ value, and execute that block. If none match, then it executes the default
block.

Each `case` contains a pattern. The first part is a value pattern
(`(Int: p, auto: _)` for example) followed by an optional boolean predicate
(`(Int p, auto _)` for example) followed by an optional boolean predicate
introduced by the `if` keyword. The value pattern has to match, and then the
predicate has to evaluate to true for the overall pattern to match. Value
patterns can be composed of the following:
Expand All @@ -80,14 +80,13 @@ patterns can be composed of the following:
identifier to bind to the value or the special identifier `_` to discard the
value once matched.
- A destructuring pattern containing a sequence of value patterns
(`(Float: x, Float: y)`) which match against tuples and tuple like values by
(`(Float x, Float y)`) which match against tuples and tuple like values by
recursively matching on their elements.
- An unwrapping pattern containing a nested value pattern which matches
against a variant or variant-like value by unwrapping it.

In order to match a value, whatever is specified in the pattern must match.
Using `auto` for a type will always match, making `auto: _` the wildcard
pattern.
Using `auto` for a type will always match, making `auto _` the wildcard pattern.

### Pattern matching in local variables

Expand All @@ -99,7 +98,7 @@ directly.
```
fn Bar() -> (Int, (Float, Float));
fn Foo() -> Int {
var (Int: p, auto: _) = Bar();
var (Int p, auto _) = Bar();
return p;
}
```
Expand Down
Loading

0 comments on commit 3149198

Please sign in to comment.