@@ -13,9 +13,9 @@ SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
13
13
14
14
## Table of contents
15
15
16
- - [ Overview ] ( #overview )
16
+ - [ Introduction ] ( #introduction )
17
17
- [ This document is provisional] ( #this-document-is-provisional )
18
- - [ Hello, Carbon ] ( #hello-carbon )
18
+ - [ Tour of the basics ] ( #tour-of-the-basics )
19
19
- [ Code and comments] ( #code-and-comments )
20
20
- [ Build modes] ( #build-modes )
21
21
- [ Types are values] ( #types-are-values )
@@ -120,7 +120,7 @@ SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
120
120
121
121
<!-- tocstop -->
122
122
123
- ## Overview
123
+ ## Introduction
124
124
125
125
This documentation describes the design of the Carbon language, and the
126
126
rationale for that design. This documentation is an overview of the Carbon
@@ -139,37 +139,201 @@ design have things that have not been decided through the Carbon process. This
139
139
preliminary material fills in gaps until aspects of the design can be filled in.
140
140
Features that are provisional have been marked as such on a best-effort basis.
141
141
142
- ## Hello, Carbon
142
+ ### Tour of the basics
143
143
144
144
Here is a simple function showing some Carbon code:
145
145
146
146
``` carbon
147
- import Console ;
147
+ import Math ;
148
148
149
- // Prints the Fibonacci numbers less than `limit`.
150
- fn Fibonacci(limit: i64) {
151
- var (a: i64, b: i64) = (0, 1);
152
- while (a < limit) {
153
- Console.Print(a, " ");
154
- let next: i64 = a + b;
155
- a = b;
156
- b = next;
149
+ // Returns the smallest factor of `n` > 1, and
150
+ // whether `n` itself is prime.
151
+ fn SmallestFactor(n: i32) -> (i32, bool) {
152
+ let limit: i32 = Math.Sqrt(n) as i32;
153
+ var i: i32 = 2;
154
+ while (i <= limit) {
155
+ let remainder: i32 = n % i;
156
+ if (remainder == 0) {
157
+ Carbon.Print("{0} is a factor of {1}", i, n);
158
+ return (i, false);
159
+ }
160
+ if (i == 2) {
161
+ i = 3;
162
+ } else {
163
+ // Skip even numbers once we get past `2`.
164
+ i += 2;
165
+ }
157
166
}
158
- Console.Print("\n" );
167
+ return (n, true );
159
168
}
160
169
```
161
170
162
171
Carbon is a language that should feel familiar to C++ and C developers. This
163
172
example has familiar constructs like [ imports] ( #imports ) ,
164
- [ function definitions] ( #functions ) , [ typed arguments] ( #binding-patterns ) , and
165
- [ curly braces] ( #blocks-and-statements ) .
173
+ [ comments] ( #code-and-comments ) , [ function definitions] ( #functions ) ,
174
+ [ typed arguments] ( #binding-patterns ) , and [ expressions] ( #expressions ) .
175
+ [ Statements] ( #blocks-and-statements ) and
176
+ [ declarations] ( #declarations-definitions-and-scopes ) are terminated with a ` ; `
177
+ or something in curly braces ` { ` ...` } ` .
166
178
167
179
A few other features that are unlike C or C++ may stand out. First,
168
180
[ declarations] ( #declarations-definitions-and-scopes ) start with introducer
169
181
keywords. ` fn ` introduces a function declaration, and ` var ` introduces a
170
- [ variable declaration] ( #variable-var-declarations ) . You can also see a
171
- [ _ tuple_ ] ( #tuples ) , a composite type written as a comma-separated list inside
172
- parentheses. Unlike, say, Python, these types are strongly-typed as well.
182
+ [ variable declaration] ( #variable-var-declarations ) .
183
+
184
+ The example starts with an [ ` import ` declaration] ( #imports ) . Carbon imports are
185
+ more like [ C++ modules] ( https://en.cppreference.com/w/cpp/language/modules ) than
186
+ [ textual inclusion during preprocessing using ` #include ` ] ( https://en.cppreference.com/w/cpp/preprocessor/include ) .
187
+ The ` import ` declaration imports a
188
+ [ library from a package] ( #files-libraries-packages ) . It must appear at the top
189
+ of a Carbon source file, the first thing after the
190
+ [ optional ` package ` declaration] ( #package-declaration ) . Libraries can optionally
191
+ be split into [ api and implementation files] ( #files-libraries-packages ) , like
192
+ C++'s header and source files but without requiring a source file in any cases.
193
+ This declaration from the example:
194
+
195
+ ``` carbon
196
+ import Math;
197
+ ```
198
+
199
+ imports the default library from package ` Math ` . The names from this library are
200
+ accessible as members of ` Math ` , like ` Math.Sqrt ` . The ` Carbon.Print ` function
201
+ comes from the ` Carbon ` package's prelude library which is
202
+ [ imported by default] ( #name-lookup-for-common-types ) . Unlike C++, the namespaces
203
+ of different packages are kept separate, so there are no name conflicts.
204
+
205
+ Carbon [ comments] ( #code-and-comments ) must be on a line by themselves starting
206
+ with ` // ` :
207
+
208
+ ``` carbon
209
+ // Returns the smallest factor of `n` > 1, and
210
+ // whether `n` itself is prime.
211
+ ...
212
+ // Skip even numbers once we get past `2`.
213
+ ```
214
+
215
+ A [ function definition] ( #functions ) consists of:
216
+
217
+ - the ` fn ` keyword introducer,
218
+ - the function's name,
219
+ - a parameter list in round parens ` ( ` ...` ) ` ,
220
+ - an optional ` -> ` and return type, and
221
+ - a body inside curly braces ` { ` ...` } ` .
222
+
223
+ ``` carbon
224
+ fn SmallestFactor(n: i32) -> (i32, bool) {
225
+ ...
226
+ return (i, false);
227
+ ...
228
+ return (n, true);
229
+ }
230
+ ```
231
+
232
+ The body of the function is an ordered sequence of
233
+ [ statements] ( #blocks-and-statements ) and
234
+ [ declarations] ( #declarations-definitions-and-scopes ) . Function execution ends
235
+ when it reaches a ` return ` statement or the end of the function body. ` return `
236
+ statements can also specify an expression whose value is returned.
237
+
238
+ Here ` i32 ` refers to a signed [ integer type] ( #integer-types ) , with 32 bits, and
239
+ ` bool ` is the [ boolean type] ( #bool ) . Carbon also has
240
+ [ floating-point types] ( #floating-point-types ) like ` f32 ` and ` f64 ` , and
241
+ [ string types] ( #string-types ) .
242
+
243
+ A [ variable declaration] ( #variable-var-declarations ) has three parts:
244
+
245
+ - the ` var ` keyword introducer,
246
+ - the name followed by a ` : ` and a type, declared the same way as a parameter
247
+ in a function signature, and
248
+ - an optional initializer.
249
+
250
+ ``` carbon
251
+ var i: i32 = 2;
252
+ ```
253
+
254
+ You can modify the value of a variable with an
255
+ [ assignment statement] ( #assignment-statements ) :
256
+
257
+ ``` carbon
258
+ i = 3;
259
+ ...
260
+ i += 2;
261
+ ```
262
+
263
+ [ Constants are declared] ( #constant-let-declarations ) with the ` let ` keyword
264
+ introducer. The syntax parallels variable declarations except the initializer is
265
+ required:
266
+
267
+ ``` carbon
268
+ let limit: i32 = Math.Sqrt(n) as i32;
269
+ ...
270
+ let remainder: i32 = n % i;
271
+ ```
272
+
273
+ The initializer ` Math.Sqrt(n) as i32 ` is an [ expression] ( #expressions ) . It first
274
+ calls the ` Math.Sqrt ` function with ` n ` as the argument. Then, the ` as ` operator
275
+ casts the floating-point return value to ` i32 ` . Lossy conversions like that must
276
+ be done explicitly.
277
+
278
+ Other expressions include ` n % i ` , which applies the binary ` % ` modulo operator
279
+ with ` n ` and ` i ` as arguments, and ` remainder == 0 ` , which applies the ` == `
280
+ comparison operator producing a ` bool ` result. Expression return values are
281
+ ignored when expressions are used as statements, as in this call to the
282
+ ` Carbon.Print ` function:
283
+
284
+ ``` carbon
285
+ Carbon.Print("{0} is a factor of {1}", i, n);
286
+ ```
287
+
288
+ Function calls consist of the name of the function followed by the
289
+ comma-separated argument list in round parentheses ` ( ` ...` ) ` .
290
+
291
+ Control flow statements, including ` if ` , ` while ` , ` for ` , ` break ` , and
292
+ ` continue ` , change the order that statements are executed, as they do in C++:
293
+
294
+ ``` carbon
295
+ while (i <= limit) {
296
+ ...
297
+ if (remainder == 0) {
298
+ ...
299
+ }
300
+ if (i == 2) {
301
+ ...
302
+ } else {
303
+ ...
304
+ }
305
+ }
306
+ ```
307
+
308
+ Every code block in curly braces ` { ` ...` } ` defines a scope. Names are visible
309
+ from their declaration until the end of innermost scope containing it. So
310
+ ` remainder ` in the example is visible until the curly brace ` } ` that closes the
311
+ ` while ` .
312
+
313
+ The example function uses a [ _ tuple_ ] ( #tuples ) , a
314
+ [ composite type] ( #composite-types ) , to return multiple values. Both tuple values
315
+ and types are written using a comma-separated list inside parentheses. So
316
+ ` (i, false) ` and ` (n, true) ` are tuple values, and ` (i32, bool) ` is their type.
317
+
318
+ [ Struct types] ( #struct-types ) are similar, except their members are referenced
319
+ by name instead of position. The example could be changed to use structs instead
320
+ as follows:
321
+
322
+ ``` carbon
323
+ // Return type of `{.factor: i32, .prime: bool}` is a struct
324
+ // with an `i32` field named `.factor`, and a `bool` field
325
+ // named `.prime`.
326
+ fn SmallestFactor(n: i32) -> {.factor: i32, .prime: bool} {
327
+ ...
328
+ if (remainder == 0) {
329
+ // Return a struct value.
330
+ return {.factor = i, .prime = false};
331
+ }
332
+ ...
333
+ // Return a struct value.
334
+ return {.factor = n, .prime = true};
335
+ }
336
+ ```
173
337
174
338
## Code and comments
175
339
@@ -190,7 +354,7 @@ required to be the only non-whitespace on the line.
190
354
> References:
191
355
>
192
356
> - [ Source files] ( code_and_name_organization/source_files.md )
193
- > - [ lexical conventions] ( lexical_conventions )
357
+ > - [ Lexical conventions] ( lexical_conventions )
194
358
> - Proposal
195
359
> [ #142 : Unicode source files] ( https://github.com/carbon-language/carbon-lang/pull/142 )
196
360
> - Proposal
@@ -213,10 +377,26 @@ The behavior of the Carbon compiler depends on the _build mode_:
213
377
214
378
Expressions compute values in Carbon, and these values are always strongly typed
215
379
much like in C++. However, an important difference from C++ is that types are
216
- themselves modeled as values; specifically, compile-time constant values. This
217
- means that the grammar for writing a type is the [ expression] ( #expressions )
218
- grammar. Expressions written where a type is expected must be able to be
219
- evaluated at compile-time and must evaluate to a type value.
380
+ themselves modeled as values; specifically, compile-time-constant values. This
381
+ has a number of consequences:
382
+
383
+ - Names for types are in the same namespace shared with functions, variables,
384
+ namespaces, and so on.
385
+ - The grammar for writing a type is the [ expression] ( #expressions ) grammar,
386
+ not a separate grammar for types. As a result, Carbon doesn't use angle
387
+ brackets ` < ` ...` > ` in types, since ` < ` and ` > ` are used for comparison in
388
+ expressions.
389
+ - Function call syntax is used to specify parameters to a type, like
390
+ ` HashMap(String, i64) ` .
391
+
392
+ Some values, such as ` () ` and ` {} ` , may even be used as types, but only act like
393
+ types when they are in a type position, like after a ` : ` in a variable
394
+ declaration or the return type after a ` -> ` in a function declaration. Any
395
+ expression in a type position must be
396
+ [ a constants or symbolic value] ( #value-categories-and-value-phases ) so the
397
+ compiler can resolve whether the value can be used as a type. This also puts
398
+ limits on how much operators can do different things for types. This is good for
399
+ consistency, but is a significant restriction on Carbon's design.
220
400
221
401
## Primitive types
222
402
@@ -567,7 +747,7 @@ Elements of an array may be accessed using square brackets (`[`...`]`), as in
567
747
568
748
``` carbon
569
749
a[i] = 2;
570
- Console .Print(a[0]);
750
+ Carbon .Print(a[0]);
571
751
```
572
752
573
753
> ** TODO:** Slices
@@ -689,9 +869,11 @@ are two kinds of patterns:
689
869
- _ Irrefutable_ patterns are guaranteed to match, so long as the code
690
870
type-checks.
691
871
692
- Irrefutable patterns are used in [ function parameters] ( #functions ) ,
872
+ In the [ introduction ] ( #tour-of-the-basics ) , [ function parameters] ( #functions ) ,
693
873
[ variable ` var ` declarations] ( #variable-var-declarations ) , and
694
- [ constant ` let ` declarations] ( #constant-let-declarations ) .
874
+ [ constant ` let ` declarations] ( #constant-let-declarations ) use a "name ` : ` type"
875
+ construction. That construction is an example of an irrefutable pattern, and in
876
+ fact any irrefutable pattern may be used in those positions.
695
877
[ ` match ` statements] ( #match ) can include both refutable patterns and irrefutable
696
878
patterns.
697
879
@@ -1051,11 +1233,11 @@ For example:
1051
1233
1052
1234
``` carbon
1053
1235
if (fruit.IsYellow()) {
1054
- Console .Print("Banana!");
1236
+ Carbon .Print("Banana!");
1055
1237
} else if (fruit.IsOrange()) {
1056
- Console .Print("Orange!");
1238
+ Carbon .Print("Orange!");
1057
1239
} else {
1058
- Console .Print("Vegetable!");
1240
+ Carbon .Print("Vegetable!");
1059
1241
}
1060
1242
```
1061
1243
@@ -1084,10 +1266,10 @@ example, this prints `0`, `1`, `2`, then `Done!`:
1084
1266
``` carbon
1085
1267
var x: i32 = 0;
1086
1268
while (x < 3) {
1087
- Console .Print(x);
1269
+ Carbon .Print(x);
1088
1270
++x;
1089
1271
}
1090
- Console .Print("Done!");
1272
+ Carbon .Print("Done!");
1091
1273
```
1092
1274
1093
1275
> References:
@@ -1103,7 +1285,7 @@ example, this prints each `String` value in `names`:
1103
1285
1104
1286
``` carbon
1105
1287
for (var name: String in names) {
1106
- Console .Print(name);
1288
+ Carbon .Print(name);
1107
1289
}
1108
1290
```
1109
1291
@@ -1123,7 +1305,7 @@ processed):
1123
1305
``` carbon
1124
1306
for (var step: Step in steps) {
1125
1307
if (step.IsManual()) {
1126
- Console .Print("Reached manual step!");
1308
+ Carbon .Print("Reached manual step!");
1127
1309
break;
1128
1310
}
1129
1311
step.Process();
@@ -1152,7 +1334,7 @@ while (!f.EOF()) {
1152
1334
if (line.IsEmpty()) {
1153
1335
continue;
1154
1336
}
1155
- Console .Print(line);
1337
+ Carbon .Print(line);
1156
1338
}
1157
1339
```
1158
1340
@@ -1181,7 +1363,7 @@ fn PrintFirstN(n: i32) {
1181
1363
// executed after a `return`.
1182
1364
return;
1183
1365
}
1184
- Console .Print(i);
1366
+ Carbon .Print(i);
1185
1367
}
1186
1368
}
1187
1369
```
@@ -2021,7 +2203,7 @@ class C {
2021
2203
// ✅ Allowed: unambiguous
2022
2204
C.F();
2023
2205
// ❌ Error: ambiguous whether `P` means
2024
- // `package.P` or `package.P.F `.
2206
+ // `package.P` or `package.C.P `.
2025
2207
P.H();
2026
2208
// ✅ Allowed
2027
2209
package.P.H();
@@ -2266,6 +2448,9 @@ imported automatically into every `api` file. Dedicated type literal syntaxes
2266
2448
like ` i32 ` and ` bool ` refer to types defined within this package, based on the
2267
2449
[ "all APIs are library APIs" principle] ( /docs/project/principles/library_apis_only.md ) .
2268
2450
2451
+ > ** TODO:** Prelude provisionally imports the ` Carbon ` package which includes
2452
+ > common facilities, like ` Print ` and the interfaces for operator overloading.
2453
+
2269
2454
> References:
2270
2455
>
2271
2456
> - [ Name lookup] ( name_lookup.md )
@@ -2420,7 +2605,7 @@ class Circle {
2420
2605
2421
2606
impl as Printable {
2422
2607
fn Print[me: Self]() {
2423
- Console.WriteLine ("Circle with radius: {0}", me.radius);
2608
+ Carbon.Print ("Circle with radius: {0}", me.radius);
2424
2609
}
2425
2610
}
2426
2611
}
0 commit comments