mirror of
https://github.com/rust-lang/rust.git
synced 2025-02-03 02:23:20 +00:00
make bind syntax unnecessary: just use _ for one of the arguments
This commit is contained in:
parent
fc023d91c7
commit
b106ef8116
@ -854,15 +854,9 @@ fn parse_bottom_expr(p: parser) -> pexpr {
|
||||
ret pexpr(mk_mac_expr(p, lo, p.span.hi, ast::mac_ellipsis));
|
||||
} else if eat_word(p, "bind") {
|
||||
let e = parse_expr_res(p, RESTRICT_NO_CALL_EXPRS);
|
||||
fn parse_expr_opt(p: parser) -> option<@ast::expr> {
|
||||
alt p.token {
|
||||
token::UNDERSCORE { p.bump(); ret none; }
|
||||
_ { ret some(parse_expr(p)); }
|
||||
}
|
||||
}
|
||||
let es =
|
||||
parse_seq(token::LPAREN, token::RPAREN, seq_sep(token::COMMA),
|
||||
parse_expr_opt, p);
|
||||
parse_expr_or_hole, p);
|
||||
hi = es.span.hi;
|
||||
ex = ast::expr_bind(e, es.node);
|
||||
} else if p.token == token::POUND {
|
||||
@ -1036,10 +1030,18 @@ fn parse_dot_or_call_expr_with(p: parser, e0: pexpr) -> pexpr {
|
||||
alt p.token {
|
||||
// expr(...)
|
||||
token::LPAREN if permits_call(p) {
|
||||
let es = parse_seq(token::LPAREN, token::RPAREN,
|
||||
seq_sep(token::COMMA), parse_expr, p);
|
||||
hi = es.span.hi;
|
||||
let nd = ast::expr_call(to_expr(e), es.node, false);
|
||||
let es_opt =
|
||||
parse_seq(token::LPAREN, token::RPAREN,
|
||||
seq_sep(token::COMMA), parse_expr_or_hole, p);
|
||||
hi = es_opt.span.hi;
|
||||
|
||||
let nd =
|
||||
if vec::any(es_opt.node, {|e| option::is_none(e) }) {
|
||||
ast::expr_bind(to_expr(e), es_opt.node)
|
||||
} else {
|
||||
let es = vec::map(es_opt.node) {|e| option::get(e) };
|
||||
ast::expr_call(to_expr(e), es, false)
|
||||
};
|
||||
e = mk_pexpr(p, lo, hi, nd);
|
||||
}
|
||||
|
||||
@ -1388,6 +1390,13 @@ fn parse_expr(p: parser) -> @ast::expr {
|
||||
ret parse_expr_res(p, UNRESTRICTED);
|
||||
}
|
||||
|
||||
fn parse_expr_or_hole(p: parser) -> option<@ast::expr> {
|
||||
alt p.token {
|
||||
token::UNDERSCORE { p.bump(); ret none; }
|
||||
_ { ret some(parse_expr(p)); }
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_expr_res(p: parser, r: restriction) -> @ast::expr {
|
||||
let old = p.restriction;
|
||||
p.restriction = r;
|
||||
|
@ -846,7 +846,12 @@ fn print_expr(s: ps, &&expr: @ast::expr) {
|
||||
_ { word(s.s, "_"); }
|
||||
}
|
||||
}
|
||||
word_nbsp(s, "bind");
|
||||
|
||||
// "bind" keyword is only needed if there are no "_" arguments.
|
||||
if !vec::any(args) {|arg| option::is_none(arg) } {
|
||||
word_nbsp(s, "bind");
|
||||
}
|
||||
|
||||
print_expr(s, func);
|
||||
popen(s);
|
||||
commasep(s, inconsistent, args, print_opt);
|
||||
|
@ -1,7 +1,7 @@
|
||||
fn main() {
|
||||
fn echo<T>(c: int, x: fn@(T)) { #error("wee"); }
|
||||
|
||||
let y = bind echo(42, _);
|
||||
let y = echo(42, _);
|
||||
|
||||
y(fn@(&&i: str) { });
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
fn main() {
|
||||
fn echo<T>(c: int, x: [T]) { }
|
||||
|
||||
let y: fn@([int]) = bind echo(42, _);
|
||||
let y: fn@([int]) = echo(42, _);
|
||||
|
||||
y([1]);
|
||||
}
|
||||
|
@ -5,7 +5,7 @@
|
||||
fn f(n: int) -> int { ret n; }
|
||||
|
||||
fn main() {
|
||||
let g: fn@(int) -> int = bind f(_);
|
||||
let g: fn@(int) -> int = f(_);
|
||||
let i: int = g(42);
|
||||
assert (i == 42);
|
||||
}
|
||||
|
@ -2,4 +2,4 @@
|
||||
|
||||
fn f(x: @int) { }
|
||||
|
||||
fn main() { let x = @10; let ff = bind f(_); ff(x); ff(x); }
|
||||
fn main() { let x = @10; let ff = f(_); ff(x); ff(x); }
|
||||
|
@ -2,4 +2,4 @@
|
||||
|
||||
fn f<T>(i: @uint, t: T) { }
|
||||
|
||||
fn main() { let x = bind f::<char>(@0xdeafbeefu, _); }
|
||||
fn main() { let x = f::<char>(@0xdeafbeefu, _); }
|
||||
|
@ -11,7 +11,7 @@ fn test_generic<T>(expected: @T, eq: compare<T>) {
|
||||
|
||||
fn test_box() {
|
||||
fn compare_box(b1: @bool, b2: @bool) -> bool { ret *b1 == *b2; }
|
||||
let eq = bind compare_box(_, _);
|
||||
let eq = compare_box(_, _);
|
||||
test_generic::<bool>(@true, eq);
|
||||
}
|
||||
|
||||
|
@ -11,7 +11,7 @@ fn test_generic<T: copy>(expected: T, eq: compare<T>) {
|
||||
|
||||
fn test_vec() {
|
||||
fn compare_box(&&v1: @int, &&v2: @int) -> bool { ret v1 == v2; }
|
||||
let eq = bind compare_box(_, _);
|
||||
let eq = compare_box(_, _);
|
||||
test_generic::<@int>(@1, eq);
|
||||
}
|
||||
|
||||
|
@ -10,7 +10,7 @@ fn test_generic<T: copy>(expected: ~T, eq: compare<T>) {
|
||||
|
||||
fn test_box() {
|
||||
fn compare_box(b1: ~bool, b2: ~bool) -> bool { ret *b1 == *b2; }
|
||||
let eq = bind compare_box(_, _);
|
||||
let eq = compare_box(_, _);
|
||||
test_generic::<bool>(~true, eq);
|
||||
}
|
||||
|
||||
|
@ -11,7 +11,7 @@ fn test_generic<T: copy>(expected: T, eq: compare<T>) {
|
||||
|
||||
fn test_vec() {
|
||||
fn compare_box(&&v1: ~int, &&v2: ~int) -> bool { ret v1 == v2; }
|
||||
let eq = bind compare_box(_, _);
|
||||
let eq = compare_box(_, _);
|
||||
test_generic::<~int>(~1, eq);
|
||||
}
|
||||
|
||||
|
@ -11,7 +11,7 @@ fn test_generic<T: copy>(expected: T, eq: compare<T>) {
|
||||
|
||||
fn test_bool() {
|
||||
fn compare_bool(&&b1: bool, &&b2: bool) -> bool { ret b1 == b2; }
|
||||
let eq = bind compare_bool(_, _);
|
||||
let eq = compare_bool(_, _);
|
||||
test_generic::<bool>(true, eq);
|
||||
}
|
||||
|
||||
@ -19,7 +19,7 @@ fn test_rec() {
|
||||
type t = {a: int, b: int};
|
||||
|
||||
fn compare_rec(t1: t, t2: t) -> bool { ret t1 == t2; }
|
||||
let eq = bind compare_rec(_, _);
|
||||
let eq = compare_rec(_, _);
|
||||
test_generic::<t>({a: 1, b: 2}, eq);
|
||||
}
|
||||
|
||||
|
@ -15,7 +15,7 @@ fn test_box() {
|
||||
log(debug, *b2);
|
||||
ret *b1 == *b2;
|
||||
}
|
||||
let eq = bind compare_box(_, _);
|
||||
let eq = compare_box(_, _);
|
||||
test_generic::<bool>(@true, eq);
|
||||
}
|
||||
|
||||
|
@ -11,7 +11,7 @@ fn test_generic<T: copy>(expected: T, eq: compare<T>) {
|
||||
|
||||
fn test_vec() {
|
||||
fn compare_vec(&&v1: @int, &&v2: @int) -> bool { ret v1 == v2; }
|
||||
let eq = bind compare_vec(_, _);
|
||||
let eq = compare_vec(_, _);
|
||||
test_generic::<@int>(@1, eq);
|
||||
}
|
||||
|
||||
|
@ -14,7 +14,7 @@ fn test_box() {
|
||||
log(debug, *b2);
|
||||
ret *b1 == *b2;
|
||||
}
|
||||
let eq = bind compare_box(_, _);
|
||||
let eq = compare_box(_, _);
|
||||
test_generic::<bool>(~true, eq);
|
||||
}
|
||||
|
||||
|
@ -11,7 +11,7 @@ fn test_generic<T: copy>(expected: T, eq: compare<T>) {
|
||||
|
||||
fn test_vec() {
|
||||
fn compare_vec(&&v1: ~int, &&v2: ~int) -> bool { ret v1 == v2; }
|
||||
let eq = bind compare_vec(_, _);
|
||||
let eq = compare_vec(_, _);
|
||||
test_generic::<~int>(~1, eq);
|
||||
}
|
||||
|
||||
|
@ -13,7 +13,7 @@ fn test_generic<T: copy>(expected: T, eq: compare<T>) {
|
||||
|
||||
fn test_bool() {
|
||||
fn compare_bool(&&b1: bool, &&b2: bool) -> bool { ret b1 == b2; }
|
||||
let eq = bind compare_bool(_, _);
|
||||
let eq = compare_bool(_, _);
|
||||
test_generic::<bool>(true, eq);
|
||||
}
|
||||
|
||||
@ -21,7 +21,7 @@ fn test_rec() {
|
||||
type t = {a: int, b: int};
|
||||
|
||||
fn compare_rec(t1: t, t2: t) -> bool { ret t1 == t2; }
|
||||
let eq = bind compare_rec(_, _);
|
||||
let eq = compare_rec(_, _);
|
||||
test_generic::<t>({a: 1, b: 2}, eq);
|
||||
}
|
||||
|
||||
|
@ -11,7 +11,7 @@ fn test_generic<T>(expected: @T, not_expected: @T, eq: compare<T>) {
|
||||
|
||||
fn test_box() {
|
||||
fn compare_box(b1: @bool, b2: @bool) -> bool { ret *b1 == *b2; }
|
||||
let eq = bind compare_box(_, _);
|
||||
let eq = compare_box(_, _);
|
||||
test_generic::<bool>(@true, @false, eq);
|
||||
}
|
||||
|
||||
|
@ -11,7 +11,7 @@ fn test_generic<T: copy>(expected: T, not_expected: T, eq: compare<T>) {
|
||||
|
||||
fn test_vec() {
|
||||
fn compare_box(&&v1: @int, &&v2: @int) -> bool { ret v1 == v2; }
|
||||
let eq = bind compare_box(_, _);
|
||||
let eq = compare_box(_, _);
|
||||
test_generic::<@int>(@1, @2, eq);
|
||||
}
|
||||
|
||||
|
@ -13,7 +13,7 @@ fn test_generic<T: copy>(expected: T, not_expected: T, eq: compare<T>) {
|
||||
|
||||
fn test_bool() {
|
||||
fn compare_bool(&&b1: bool, &&b2: bool) -> bool { ret b1 == b2; }
|
||||
let eq = bind compare_bool(_, _);
|
||||
let eq = compare_bool(_, _);
|
||||
test_generic::<bool>(true, false, eq);
|
||||
}
|
||||
|
||||
@ -21,7 +21,7 @@ fn test_rec() {
|
||||
type t = {a: int, b: int};
|
||||
|
||||
fn compare_rec(t1: t, t2: t) -> bool { ret t1 == t2; }
|
||||
let eq = bind compare_rec(_, _);
|
||||
let eq = compare_rec(_, _);
|
||||
test_generic::<t>({a: 1, b: 2}, {a: 2, b: 3}, eq);
|
||||
}
|
||||
|
||||
|
@ -1,9 +1,9 @@
|
||||
fn fix_help<A, B>(f: native fn(fn@(A) -> B, A) -> B, x: A) -> B {
|
||||
ret f(bind fix_help(f, _), x);
|
||||
ret f(fix_help(f, _), x);
|
||||
}
|
||||
|
||||
fn fix<A, B>(f: native fn(fn@(A) -> B, A) -> B) -> fn@(A) -> B {
|
||||
ret bind fix_help(f, _);
|
||||
ret fix_help(f, _);
|
||||
}
|
||||
|
||||
fn fact_(f: fn@(&&int) -> int, &&n: int) -> int {
|
||||
|
@ -1,9 +1,9 @@
|
||||
fn fix_help<A, B: send>(f: native fn(fn@(A) -> B, A) -> B, x: A) -> B {
|
||||
ret f(bind fix_help(f, _), x);
|
||||
ret f(fix_help(f, _), x);
|
||||
}
|
||||
|
||||
fn fix<A, B: send>(f: native fn(fn@(A) -> B, A) -> B) -> fn@(A) -> B {
|
||||
ret bind fix_help(f, _);
|
||||
ret fix_help(f, _);
|
||||
}
|
||||
|
||||
fn fact_(f: fn@(&&int) -> int, &&n: int) -> int {
|
||||
|
@ -7,7 +7,7 @@ fn main() {
|
||||
let a: int = direct(3); // direct
|
||||
let b: int = ho(direct); // indirect unbound
|
||||
|
||||
let c: int = ho(bind direct(_)); // indirect bound
|
||||
let c: int = ho(direct(_)); // indirect bound
|
||||
assert (a == b);
|
||||
assert (b == c);
|
||||
}
|
||||
|
@ -6,12 +6,12 @@ fn main() {
|
||||
let t = {_0: 1, _1: 2, _2: 3, _3: 4, _4: 5, _5: 6, _6: 7};
|
||||
assert (t._5 == 6);
|
||||
let f1 =
|
||||
bind id::<{_0: int,
|
||||
_1: int,
|
||||
_2: int,
|
||||
_3: int,
|
||||
_4: int,
|
||||
_5: int,
|
||||
_6: int}>(_);
|
||||
id::<{_0: int,
|
||||
_1: int,
|
||||
_2: int,
|
||||
_3: int,
|
||||
_4: int,
|
||||
_5: int,
|
||||
_6: int}>(_);
|
||||
assert (f1(t)._5 == 6);
|
||||
}
|
||||
|
@ -58,7 +58,7 @@ mod map_reduce {
|
||||
}
|
||||
}
|
||||
|
||||
map(input, bind emit(intermediates, ctrl, _, _));
|
||||
map(input, emit(intermediates, ctrl, _, _));
|
||||
send(ctrl, mapper_done);
|
||||
}
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
fn quux<T: copy>(x: T) -> T { let f = bind id::<T>(_); ret f(x); }
|
||||
fn quux<T: copy>(x: T) -> T { let f = id::<T>(_); ret f(x); }
|
||||
|
||||
fn id<T: copy>(x: T) -> T { ret x; }
|
||||
|
||||
|
@ -7,5 +7,5 @@ fn log_if<T>(c: native fn(T)->bool, e: T) {
|
||||
}
|
||||
|
||||
fn main() {
|
||||
(bind log_if(even, _))(2);
|
||||
(log_if(even, _))(2);
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
fn add(i: int, j: int) -> int { ret i + j; }
|
||||
fn binder(n: int) -> fn@() -> int { let f = bind add(n, _); ret bind f(2); }
|
||||
fn binder(n: int) -> fn@() -> int { let f = add(n, _); ret bind f(2); }
|
||||
fn main() {
|
||||
binder(5);
|
||||
let f = binder(1);
|
||||
|
@ -1,9 +1,6 @@
|
||||
// xfail-test
|
||||
// Issue #922
|
||||
|
||||
// This test is specifically about spawning temporary closures, which
|
||||
// isn't possible under the bare-fn regime. I'm keeping it around
|
||||
// until such time as we have unique closures.
|
||||
// This test is specifically about spawning temporary closures.
|
||||
|
||||
use std;
|
||||
import task;
|
||||
@ -12,5 +9,5 @@ fn f() {
|
||||
}
|
||||
|
||||
fn main() {
|
||||
task::spawn(bind f());
|
||||
task::spawn {|| f() };
|
||||
}
|
@ -18,7 +18,7 @@ fn pure_foldl<T: copy, U: copy>(ls: list<T>, u: U, f: fn(T, U) -> U) -> U {
|
||||
// fn from a pure fn
|
||||
pure fn pure_length<T: copy>(ls: list<T>) -> uint {
|
||||
fn count<T>(_t: T, &&u: uint) -> uint { u + 1u }
|
||||
unchecked{ pure_foldl(ls, 0u, bind count(_, _)) }
|
||||
unchecked{ pure_foldl(ls, 0u, count(_, _)) }
|
||||
}
|
||||
|
||||
pure fn nonempty_list<T: copy>(ls: list<T>) -> bool { pure_length(ls) > 0u }
|
||||
|
Loading…
Reference in New Issue
Block a user