You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: apps/nextra/pages/en/build/smart-contracts/book/enums.mdx
+9-11
Original file line number
Diff line number
Diff line change
@@ -95,8 +95,8 @@ Notice above that the value matched is an immutable reference to an enum value.
95
95
```move
96
96
fun scale_radius(self: &mut Rectangle, factor: u64) {
97
97
match (self) {
98
-
Circle{radius} => *radius = *radius * factor,
99
-
_ => {} // do nothing if not a Circle
98
+
Circle{radius: r} => *r = *r * factor,
99
+
_ => {} // do nothing if not a Circle
100
100
}
101
101
}
102
102
```
@@ -106,19 +106,17 @@ The patterns provided in the match expression are evaluated sequentially, in ord
106
106
Patterns can be nested and contain conditions, as in the following example:
107
107
108
108
```move
109
-
use Result::*;
110
109
let r : Result<Result<u64>> = Ok(Err(42));
111
110
let v = match (r)) {
112
111
Ok(Err(c)) if c < 42 => 0,
113
112
Ok(Err(c)) if c >= 42 => 1,
114
113
Ok(_) => 2,
115
-
_ => 3
114
+
_ => 3
115
+
};
116
116
assert!(v == 1);
117
117
```
118
118
119
-
Notice that in the above example, the last match clause (`_`) covers both patterns `Ok(Err(_))` and `Err(_)`. Although at execution time, the earlier clauses may match `Ok(Err(c))` for all values of `c`, the compiler cannot be sure all cases are covered due to the conditionals: conditions in match expressions are not considered when tracking coverage. Thus the first two clauses in the match expression above are not sufficient for match completeness, and an additional clause is required to avoid a compiler error.
120
-
121
-
Notice that while match completeness is enforced by the compiler, at runtime it may not be guaranteed if code is upgraded. See the discussion of upgrade compatibility of enums here.
119
+
Notice that in the above example, the last match clause (`_`) covers both patterns `Ok(Err(_))` and `Err(_)`. Although at execution time, the earlier clauses match `Ok(Err(c))` for all values of `c`, the compiler cannot be sure all cases are covered due to the conditionals: conditions in match expressions are not considered when tracking coverage. Thus the first two clauses in the match expression above are not sufficient for match completeness, and an additional clause is required to avoid a compiler error.
122
120
123
121
## Testing Enum Variants
124
122
@@ -135,7 +133,6 @@ The operator allows specifying a list of variants, separated by "`|`" characters
135
133
assert!(data is V1|V2);
136
134
```
137
135
138
-
139
136
## Selecting From Enum Values
140
137
141
138
It is possible to directly select a field from an enum value. Recall the definition of versioned data:
@@ -153,19 +150,20 @@ One can write code as below to directly select the fields of variants:
153
150
let s: String;
154
151
let data1 = VersionedData::V1{name: s};
155
152
let data2 = VersionedData::V2{name: s, age: 20};
156
-
assert!(data1 is VersionData::V2);
157
153
assert!(data1.name == data2.name)
158
-
assert!(data2.age == 20); // Note that `data1.age` will abort!
154
+
assert!(data2.age == 20);
159
155
```
160
156
161
157
Notice that field selection aborts if the enum value has no variant with the given field. This is the case for `data1.age`.
158
+
The abort code used for this case is `0xCA26CBD9BE0B0001`. In terms of the `std::error` convention, this code has
159
+
category `std::error::INTERNAL` and reason `1`.
162
160
163
161
Field selection is only possible if the field is uniquely named and typed throughout all variants. Thus, the following yields a compile time error:
Copy file name to clipboardexpand all lines: apps/nextra/pages/en/build/smart-contracts/book/functions.mdx
+7-4
Original file line number
Diff line number
Diff line change
@@ -88,7 +88,7 @@ A `package` function can be only called within the same package. The notion of a
88
88
defined by the hosting environment of Move, and not explicit in the language. Typically, the package
89
89
is defined by a manifest file `Move.toml` which is processed by the build environment.
90
90
91
-
The following works, provided the two modules belong to the same package:
91
+
The following works, provided the two modules belong to the same package and are at the same address:
92
92
93
93
```move
94
94
module 0x42::m {
@@ -104,7 +104,10 @@ module 0x42::other {
104
104
105
105
An attempt to access `0x42::m::foo` from another package will fail at compile time.
106
106
107
-
Notice that package visibility is a compile time concept which is reduced by the compiler to friend visibility (described [below](#friend-visibility)), which can be verified by the Move VM.
107
+
In addition to the notation `package fun`, also the longer notation `public(package) fun` is supported.
108
+
109
+
Notice that package visibility is a compile time concept which is reduced by the compiler to friend visibility (described [below](#friend-visibility)), which can be verified by the Move VM. The Move VM guarantees that friend functions
110
+
cannot be called across address boundaries, independent of what package system a compilation environment supports.
108
111
109
112
110
113
#### `public(friend)` visibility
@@ -114,9 +117,9 @@ _Since Language Version 2.0_, `friend fun` replaces `public(friend) fun`. The ol
114
117
The `public(friend)` visibility modifier is a more restricted form of the `public` modifier to give more control about where a function can be used. A `public(friend)` function can be called by:
115
118
116
119
- other functions defined in the same module, or
117
-
- functions defined in modules which are explicitly specified in the **friend list** (see [Friends](friends.mdx) on how to specify the friend list).
120
+
- functions defined in modules which are explicitly specified in the **friend list** (see [Friends](friends.mdx) on how to specify the friend list), and which reside at the same address.
118
121
119
-
Note that since we cannot declare a script to be a friend of a module, the functions defined in scripts can never call a `public(friend)` function.
122
+
Note that since we cannot declare a script to be a friend of a module, the functions defined in scripts can never call a `public(friend)` function.
0 commit comments