On Sat, Jun 2, 2012 at 9:12 PM, Niko Matsakis <n...@alum.mit.edu> wrote:

> 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
> Rust-dev@mozilla.org
> https://mail.mozilla.org/**listinfo/rust-dev<https://mail.mozilla.org/listinfo/rust-dev>
>

Hello,

I must admit I find the latter example quite pleasing: foo.map(|x| (x, x))
and (unlike you it seems) finds that it could completely replace _.

The problem with _ is that though it seems nice enough if there is one, but
it is not as immediate for the reader to determine how many parameters the
resulting function has. We could number them (_0, _1, _2) but it makes
maintenance painful (if you remove _1, you have to remain all those which
followed).

I find that explicitly naming the arguments in between the pipes really
help making it clear how many arguments the created function has.

Of course, it still does nothing with the problem of shadowing an outer
element meant to be captured. Not sure if there is a way to deal with that
at the same time...

-- Matthieu
_______________________________________________
Rust-dev mailing list
Rust-dev@mozilla.org
https://mail.mozilla.org/listinfo/rust-dev

Reply via email to