librustc: Implement |A| -> B syntax for closures and make bare fn
work
This commit is contained in:
parent
e976de32dc
commit
f27272d60f
10 changed files with 320 additions and 57 deletions
|
|
@ -19,5 +19,5 @@ fn main() {
|
|||
}
|
||||
|
||||
f(g);
|
||||
//~^ ERROR mismatched types: expected `extern "Rust" fn(extern "Rust" fn(extern "Rust" fn()))`
|
||||
//~^ ERROR mismatched types: expected `fn(fn(fn()))`
|
||||
}
|
||||
|
|
|
|||
13
src/test/compile-fail/closure-reform-bad.rs
Normal file
13
src/test/compile-fail/closure-reform-bad.rs
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
/* Any copyright is dedicated to the Public Domain.
|
||||
* http://creativecommons.org/publicdomain/zero/1.0/ */
|
||||
|
||||
fn call_bare(f: fn(&str)) {
|
||||
f("Hello ");
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let string = "world!";
|
||||
let f: |&str| = |s| println(s + string);
|
||||
call_bare(f) //~ ERROR mismatched types
|
||||
}
|
||||
|
||||
17
src/test/pretty/closure-reform-pretty.rs
Normal file
17
src/test/pretty/closure-reform-pretty.rs
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
// Any copyright is dedicated to the Public Domain.
|
||||
// http://creativecommons.org/publicdomain/zero/1.0/
|
||||
|
||||
// pp-exact
|
||||
|
||||
fn call_it(f: proc(~str) -> ~str) { }
|
||||
|
||||
fn call_this(f: |&str|: Send) { }
|
||||
|
||||
fn call_that(f: <'a>|&'a int, &'a int|: -> int) { }
|
||||
|
||||
fn call_extern(f: fn() -> int) { }
|
||||
|
||||
fn call_abid_extern(f: extern "C" fn() -> int) { }
|
||||
|
||||
pub fn main() { }
|
||||
|
||||
|
|
@ -14,7 +14,7 @@
|
|||
// preserved. They are needed to disambiguate `{return n+1}; - 0` from
|
||||
// `({return n+1}-0)`.
|
||||
|
||||
fn id(f: &fn() -> int) -> int { f() }
|
||||
fn id(f: || -> int) -> int { f() }
|
||||
|
||||
fn wsucc(_n: int) -> int { (do id || { 1 }) - 0 }
|
||||
fn main() { }
|
||||
|
|
|
|||
|
|
@ -10,6 +10,6 @@
|
|||
|
||||
// pp-exact
|
||||
|
||||
fn f(f: &fn(int)) { f(10) }
|
||||
fn f(f: |int|) { f(10) }
|
||||
|
||||
fn main() { do f |i| { assert!(i == 10) } }
|
||||
|
|
|
|||
|
|
@ -10,7 +10,7 @@
|
|||
|
||||
// pp-exact
|
||||
|
||||
fn from_foreign_fn(_x: extern "Rust" fn()) { }
|
||||
fn from_stack_closure(_x: &fn()) { }
|
||||
fn from_foreign_fn(_x: fn()) { }
|
||||
fn from_stack_closure(_x: ||) { }
|
||||
fn from_unique_closure(_x: ~fn()) { }
|
||||
fn main() { }
|
||||
|
|
|
|||
|
|
@ -1,11 +1,42 @@
|
|||
/* Any copyright is dedicated to the Public Domain.
|
||||
* http://creativecommons.org/publicdomain/zero/1.0/ */
|
||||
|
||||
use std::cast;
|
||||
|
||||
fn call_it(f: proc(~str) -> ~str) {
|
||||
println(f(~"Fred"))
|
||||
}
|
||||
|
||||
fn call_a_thunk(f: ||) {
|
||||
f();
|
||||
}
|
||||
|
||||
fn call_this(f: |&str|:Send) {
|
||||
f("Hello!");
|
||||
}
|
||||
|
||||
fn call_that(f: <'a>|&'a int, &'a int|: -> int) {
|
||||
let (ten, forty_two) = (10, 42);
|
||||
println!("Your lucky number is {}", f(&ten, &forty_two));
|
||||
}
|
||||
|
||||
fn call_cramped(f:||->uint,g:<'a>||->&'a uint) {
|
||||
let number = f();
|
||||
let other_number = *g();
|
||||
println!("Ticket {} wins an all-expenses-paid trip to Mountain View", number + other_number);
|
||||
}
|
||||
|
||||
fn call_bare(f: fn(&str)) {
|
||||
f("Hello world!")
|
||||
}
|
||||
|
||||
fn call_bare_again(f: extern "Rust" fn(&str)) {
|
||||
f("Goodbye world!")
|
||||
}
|
||||
|
||||
pub fn main() {
|
||||
// Procs
|
||||
|
||||
let greeting = ~"Hi ";
|
||||
do call_it |s| {
|
||||
greeting + s
|
||||
|
|
@ -23,5 +54,26 @@ pub fn main() {
|
|||
call_it(proc(s: ~str) -> ~str {
|
||||
greeting + s
|
||||
});
|
||||
|
||||
// Closures
|
||||
|
||||
call_a_thunk(|| println("Hello world!"));
|
||||
|
||||
call_this(|s| println(s));
|
||||
|
||||
call_that(|x, y| *x + *y);
|
||||
|
||||
let z = 100;
|
||||
call_that(|x, y| *x + *y - z);
|
||||
|
||||
call_cramped(|| 1, || unsafe {
|
||||
cast::transmute(&100)
|
||||
});
|
||||
|
||||
// External functions
|
||||
|
||||
call_bare(println);
|
||||
|
||||
call_bare_again(println);
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue