Hello Rusters,
I want to remove bind. It's a pain to maintain and a significant
complication in the language semantics (both problems caused by having
two kinds of closures with subtle distinctions). Nobody really objects
to this, but the fact remains that bind serves a role that is otherwise
unfilled: it provides a (relatively) lightweight closure syntax. For
example, I can write:
foo.map(bind some_func(_, 3))
which is significantly less noisy than:
foo.map({|x| some_func(x, 3)})
I previously tried to address this through the introduction of `_`
expressions as a shorthand for closures. This proposal was eventually
rejected because the scope of the closure was unclear. I have an
alternative I've been thinking about lately.
The basic idea is to introduce a new closure expression which looks
like: `|| expr`. The expression `expr` may make use of `_` to indicate
anonymous arguments. The scope of the closure is basically greedy. So
`|| _ + _ * _` is unproblematic. The double bars `||` should signal
that this is a closure.
Therefore, you could write the above example:
foo.map(|| some_func(_, 3))
This also makes for a nice, lightweight thunk syntax. So, a method like:
map.get_or_insert(key, || compute_initial_value(...))
which would (presumably) return `key` if it is present in the map, but
otherwise execute the thunk and insert the returned value.
The same convention naturally extends to named parameters, for those
cases where anonymous parameters do not work. For example, if you wish
to reference the parameter more than once, as in this example, which
pairs each item in a vector with itself ([A] => [(A,A)]):
foo.map(|x| (x, x))
In fact, we *could* do away with underscores altogether, although I find
them more readable (they highlight those portions of the function call
that come from arguments vs the environment).
The immediate motivation for this is that I am having troubles with some
code due to complications introduced by bind. I'd rather not fix those
bugs. I'd rather just delete bind.
NIko
_______________________________________________
Rust-dev mailing list
[email protected]
https://mail.mozilla.org/listinfo/rust-dev