Update Examples

Glen Whitney 2024-08-26 22:42:25 +00:00
parent a24e40cc71
commit f1b01e0437

@ -1049,6 +1049,34 @@ fn main() {
</td><td>
```
fn main
let outer_var = 42
let closure_annotated = i: i32 -> i32 => i + outer_var
let closure_inferred = i => i + outer_var
println! "closure_annotated: {}", closure_annotated 1
println! "closure_inferred: {}", closure_inferred 1
// Once closure's type has been inferred, it cannot be inferred again with another type.
//println! "cannot reuse closure_inferred with another type: {}", closure_inferred 42i64
// TODO: uncomment the line above and see the compiler error.
let one = => 1
println!("closure returning one: {}", one());
}
```
</td></tr></table>
The other big consideration is whether we want to have a shorthand for non-closure anonymous inline functions (i.e. ones that are enforced not to capture any identifiers). If so, the simplest mechanism might just be to use another arrow, i.e. Husht could look like:
```
fn main
let anon_func = x --> x*x
println! "call unknown {}", anon_func 1.6
// Exactly the same except for function name:
fn named_func(x: f64) -> f64 x* x
println! "call named {}", named_func 1.6
// Arrow looks slightly funny with type annotations:
let another = x: i64 -> i64 --> x*x
// but not too bad I think
```
Of course, there's no real reason to implement that until/unless we actually want anonymous regular (non-closure) functions.