@@ -862,6 +862,9 @@ or restrictions on casts between pointers and integers.
862
862
863
863
### Arrays and slices
864
864
865
+ > ** TODO:** The provisional array syntax documented here has been superseded by
866
+ > [ p4682: The Core.Array type for direct-storage immutably-sized buffers] ( /proposals/p4682.md ) .
867
+
865
868
The type of an array of holding 4 ` i32 ` values is written ` [i32; 4] ` . There is
866
869
an [ implicit conversion] ( expressions/implicit_conversions.md ) from tuples to
867
870
arrays of the same length as long as every component of the tuple may be
@@ -1764,16 +1767,16 @@ class Point {
1764
1767
return Math.Sqrt(dx * dx + dy * dy);
1765
1768
}
1766
1769
// Mutating method declaration
1767
- fn Offset[addr self: Self* ](dx: i32, dy: i32);
1770
+ fn Offset[ref self: Self](dx: i32, dy: i32);
1768
1771
1769
1772
var x: i32;
1770
1773
var y: i32;
1771
1774
}
1772
1775
1773
1776
// Out-of-line definition of method declared inline
1774
- fn Point.Offset[addr self: Self* ](dx: i32, dy: i32) {
1775
- self-> x += dx;
1776
- self-> y += dy;
1777
+ fn Point.Offset[ref self: Self](dx: i32, dy: i32) {
1778
+ self. x += dx;
1779
+ self. y += dy;
1777
1780
}
1778
1781
1779
1782
var origin: Point = {.x = 0, .y = 0};
@@ -1793,10 +1796,9 @@ two methods `Distance` and `Offset`:
1793
1796
- ` Distance ` computes and returns the distance to another point, without
1794
1797
modifying the ` Point ` . This is signified using ` [self: Self] ` in the method
1795
1798
declaration.
1796
- - ` origin.Offset( ` ...` ) ` does modify the value of ` origin ` . This is signified
1797
- using ` [addr self: Self*] ` in the method declaration. Since calling this
1798
- method requires taking the [ non-` const ` ] ( #const ) address of ` origin ` , it may
1799
- only be called on [ reference expressions] ( #expression-categories ) .
1799
+ - ` origin.Offset( ` ...` ) ` _ does_ modify the value of ` origin ` . This is
1800
+ signified using ` [ref self: Self] ` in the method declaration. It may only be
1801
+ called on [ reference expressions] ( #expression-categories ) .
1800
1802
- Methods may be declared lexically inline like ` Distance ` , or lexically out
1801
1803
of line like ` Offset ` .
1802
1804
@@ -1952,7 +1954,7 @@ names resolvable by the compiler, and don't act like forward declarations.
1952
1954
1953
1955
A destructor for a class is custom code executed when the lifetime of a value of
1954
1956
that type ends. They are defined with ` fn destroy ` followed by either
1955
- ` [self: Self] ` or ` [addr self: Self* ] ` (as is done with [ methods] ( #methods ) ) and
1957
+ ` [self: Self] ` or ` [ref self: Self] ` (as is done with [ methods] ( #methods ) ) and
1956
1958
the block of code in the class definition, as in:
1957
1959
1958
1960
``` carbon
@@ -1966,7 +1968,7 @@ or:
1966
1968
``` carbon
1967
1969
class MyClass {
1968
1970
// Can modify `self` in the body.
1969
- fn destroy[addr self: Self* ]() { ... }
1971
+ fn destroy[ref self: Self]() { ... }
1970
1972
}
1971
1973
```
1972
1974
@@ -2001,8 +2003,8 @@ For every type `MyClass`, there is the type `const MyClass` such that:
2001
2003
has type ` const T ` .
2002
2004
- While all of the member names in ` MyClass ` are also member names in
2003
2005
` const MyClass ` , the effective API of a ` const MyClass ` reference expression
2004
- is a subset of ` MyClass ` , because only ` addr ` methods accepting a
2005
- ` const Self* ` will be valid.
2006
+ is a subset of ` MyClass ` , because only ` ref ` methods accepting a
2007
+ ` const Self ` will be valid.
2006
2008
2007
2009
Note that ` const ` binds more tightly than postfix-` * ` for forming a pointer
2008
2010
type, so ` const MyClass* ` is equal to ` (const MyClass)* ` .
@@ -2993,8 +2995,8 @@ associated constant to represent the type of elements stored in the stack.
2993
2995
```
2994
2996
interface StackInterface {
2995
2997
let ElementType:! Movable;
2996
- fn Push[addr self: Self* ](value: ElementType);
2997
- fn Pop[addr self: Self* ]() -> ElementType;
2998
+ fn Push[ref self: Self](value: ElementType);
2999
+ fn Pop[ref self: Self]() -> ElementType;
2998
3000
fn IsEmpty[self: Self]() -> bool;
2999
3001
}
3000
3002
```
@@ -3005,14 +3007,14 @@ for the `ElementType` member of the interface using a `where` clause:
3005
3007
``` carbon
3006
3008
class IntStack {
3007
3009
extend impl as StackInterface where .ElementType = i32 {
3008
- fn Push[addr self: Self* ](value: i32);
3010
+ fn Push[ref self: Self](value: i32);
3009
3011
// ...
3010
3012
}
3011
3013
}
3012
3014
3013
3015
class FruitStack {
3014
3016
extend impl as StackInterface where .ElementType = Fruit {
3015
- fn Push[addr self: Self* ](value: Fruit);
3017
+ fn Push[ref self: Self](value: Fruit);
3016
3018
// ...
3017
3019
}
3018
3020
}
@@ -3040,8 +3042,8 @@ values of any type `T`:
3040
3042
3041
3043
``` carbon
3042
3044
class Stack(T:! type) {
3043
- fn Push[addr self: Self* ](value: T);
3044
- fn Pop[addr self: Self* ]() -> T;
3045
+ fn Push[ref self: Self](value: T);
3046
+ fn Pop[ref self: Self]() -> T;
3045
3047
3046
3048
var storage: Array(T);
3047
3049
}
0 commit comments