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
If the name `output` collides with an existing identifier, you can choose a different name for it in two ways:
116
116
117
-
**1. Contract-Wide Name**: Use the `binds` parameter to set a new name for the return value across all postconditions in the contract.
117
+
**1. Contract-Wide Binding**: Use the `binds` parameter to set a new name for the return value across all postconditions in the contract.
118
118
119
119
```rust
120
120
#[contract(
121
121
binds: new_value,
122
122
ensures: new_value > old_value,
123
123
)]
124
-
fnincrement(old_value:i32) ->i32 { old_value+1 }
124
+
fnincrement(old_value:i32) ->i32 { /* ... */ }
125
125
```
126
126
127
-
**2. Closure Argument Name**: Write the postcondition as a closure. This has the highest precedence and affects only that single condition.
127
+
**2. Closure Binding**: Write the postcondition as a closure. This has the highest precedence and affects only that single condition.
128
128
129
129
```rust
130
130
#[contract(
131
-
// This postcondition uses the default name `output`.
131
+
// This postcondition uses the default binding `output`.
132
132
ensures: output.is_valid(),
133
-
// This postcondition uses `val` as the name for the return value.
133
+
// This postcondition binds the return value as `val`.
134
134
ensures:|val| val.id() != 0,
135
135
)]
136
136
fncreate_data() ->Data { /* ... */ }
137
137
```
138
138
139
-
**3. Combined Name Settings**: When both are used, the name from the closure argument always takes precedence for its specific condition, while other postconditions fall back to the contract-wide name.
139
+
**3. Binding Precedence**: The closure's binding takes precedence; same as in Rust. Plain postconditions still use the contract-wide binding.
140
140
141
141
```rust
142
142
// A function where 'output' is an argument name, requiring a different name.
The `binds` parameter also lets you destructure return values, making complex postconditions easier to read and write. You can use any valid Rust pattern, including tuple patterns, struct patterns, or even more complex nested patterns.
157
157
@@ -163,7 +163,7 @@ use anodized::contract;
163
163
binds: (a, b),
164
164
// Postconditions can now use the bound variables `a` and `b`.
0 commit comments