diff --git a/Examples.md b/Examples.md index 9288497..89badf7 100644 --- a/Examples.md +++ b/Examples.md @@ -1049,6 +1049,34 @@ fn main() { ``` +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()); +} ``` + +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. \ No newline at end of file