Discussion:
[rust-dev] Closures with owned environments
Allen Welkie
2014-09-09 20:39:01 UTC
Permalink
In this stackoverflow question:

http://stackoverflow.com/questions/21130272/return-a-closure-from-a-function

Chris Morgan mentions the possibility of adding closures with owned
environments with the DST implementation. Is this being done with the
current DST effort? If not, are there plans to add it?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/rust-dev/attachments/20140909/e468170b/attachment.html>
Vladimir Matveev
2014-09-09 21:00:09 UTC
Permalink
Hi, Allen!

In fact, it is possible to do it now without DST, using unboxed closures only:

#![feature(unboxed_closures, unboxed_closure_sugar)]

fn make_adder(x: int) -> Box<|&: int| -> int> {
box |&: y: int| x + y
}

fn main() {
let f = make_adder(3);
println!("{}", f.call((4i,)));
}

Test it here: http://is.gd/mCJ6Dh

This code employs the idea that unboxed closure is just an instance of
some anonymous struct implementing one of Fn* traits, so we can just
return a boxed trait object representing the closure. The call syntax
is ugly, however, but this should change in the nearest future (though
explicit dereferencing, like (*f)(x), will likely be needed anyway).
Post by Allen Welkie
http://stackoverflow.com/questions/21130272/return-a-closure-from-a-function
Chris Morgan mentions the possibility of adding closures with owned
environments with the DST implementation. Is this being done with the
current DST effort? If not, are there plans to add it?
_______________________________________________
Rust-dev mailing list
Rust-dev at mozilla.org
https://mail.mozilla.org/listinfo/rust-dev
Allen Welkie
2014-09-09 21:17:50 UTC
Permalink
Oh, cool. Instead of returning the closure from a function, I'd rather use
it in a call to map(), then return that Map struct. Something like:

fn map_add<I: Iterator<int>>(it: I, x: int) -> Map<...> {
it.map(|&: y: int| x + y)
}

So does this mean I just need to wait until the map() function accepts
unboxed closures?

On Tue, Sep 9, 2014 at 5:00 PM, Vladimir Matveev <dpx.infinity at gmail.com>
Post by Vladimir Matveev
Hi, Allen!
#![feature(unboxed_closures, unboxed_closure_sugar)]
fn make_adder(x: int) -> Box<|&: int| -> int> {
box |&: y: int| x + y
}
fn main() {
let f = make_adder(3);
println!("{}", f.call((4i,)));
}
Test it here: http://is.gd/mCJ6Dh
This code employs the idea that unboxed closure is just an instance of
some anonymous struct implementing one of Fn* traits, so we can just
return a boxed trait object representing the closure. The call syntax
is ugly, however, but this should change in the nearest future (though
explicit dereferencing, like (*f)(x), will likely be needed anyway).
http://stackoverflow.com/questions/21130272/return-a-closure-from-a-function
Post by Allen Welkie
Chris Morgan mentions the possibility of adding closures with owned
environments with the DST implementation. Is this being done with the
current DST effort? If not, are there plans to add it?
_______________________________________________
Rust-dev mailing list
Rust-dev at mozilla.org
https://mail.mozilla.org/listinfo/rust-dev
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/rust-dev/attachments/20140909/a768d5b6/attachment.html>
Loading...