Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Method cascades #6679

Closed
dharmatech opened this issue May 22, 2013 · 17 comments
Closed

Method cascades #6679

dharmatech opened this issue May 22, 2013 · 17 comments

Comments

@dharmatech
Copy link

Consider method cascades for Rust.

@mstewartgallus
Copy link
Contributor

Is it possible to do this with a macro? It could have a syntax like:

 let address = with! (
     mut it = getAddress();
     setStreet(“Elm”, “13a”)
     setCity(“London”)
     setProvince(“Airstrip One”)
     setState(“Oceania”)
     setZip(66666, 6666)
 );

And that would desugar to something like

 let address = {
     let mut it = getAddress ();
     it.setStreet(“Elm”, “13a”);
     it.setCity(“London”);
     it.setProvince“Airstrip One”);
     it.setState("Oceania”);
.    it.setZip(66666, 6666);
     it
 };

Something along these lines could be useful to implement.

@metajack
Copy link
Contributor

This is like Clojure's doto it seems. I'm not sure it's a good idea to assign to it every time. That would basically mean the underlying calls would need to be chainable, which is not always true.

@bstrie
Copy link
Contributor

bstrie commented May 22, 2013

It's a neat concept, and I think I actually prefer the look of it to method chaining, but I'm not sure if it's really necessary for Rust.

@emberian
Copy link
Member

emberian commented Aug 5, 2013

This would be pretty cool indeed, as a syntax extension.

@Aatch
Copy link
Contributor

Aatch commented Mar 23, 2014

If anybody is still interested in this, an official RFC should be filed. https://github.com/rust-lang/rfcs/blob/master/active/0001-rfc-process.md

@Aatch Aatch closed this as completed Mar 23, 2014
@ariasuni
Copy link
Contributor

@huonw has implemented cascade operator with a macro that can be used as follow:

let value = cascade!(Foo::new()
                     .. add_to_y(2.0)
                     .. x = 2
                     .. double_x()
                     .. z = "bar".to_strbuf());

I think it’s a great addition to the language compared to the quantity of code to merge, so please re-open.

@bstrie
Copy link
Contributor

bstrie commented May 19, 2014

Before adding such a macro by default, I would like someone to demonstrate places in real Rust code that could benefit from it. An operator that is useful according to Dart's idioms is not automatically useful in Rust. Until then, this issue should not be re-opened.

@emberian
Copy link
Member

I've used a similar macro called with (emulating VB.NET's with
statement), but it was only for a toy -- I found no uses for it in real
code.

On Sun, May 18, 2014 at 7:24 PM, Ben Striegel [email protected]:

Before adding such a macro by default, I would like someone to demonstrate
places in real Rust code that could benefit from it. An operator that is
useful according to Dart's idioms is not automatically useful in Rust.
Until then, this issue should not be re-opened.


Reply to this email directly or view it on GitHubhttps://github.com//issues/6679#issuecomment-43461647
.

http://octayn.net/

@emberian
Copy link
Member

(Especially since we have functional record update)

On Sun, May 18, 2014 at 7:35 PM, Corey Richardson [email protected] wrote:

I've used a similar macro called with (emulating VB.NET's with
statement), but it was only for a toy -- I found no uses for it in real
code.

On Sun, May 18, 2014 at 7:24 PM, Ben Striegel [email protected]:

Before adding such a macro by default, I would like someone to
demonstrate places in real Rust code that could benefit from it. An
operator that is useful according to Dart's idioms is not automatically
useful in Rust. Until then, this issue should not be re-opened.


Reply to this email directly or view it on GitHubhttps://github.com//issues/6679#issuecomment-43461647
.

http://octayn.net/

http://octayn.net/

@dharmatech
Copy link
Author

draw_push_clip as it currently is:

https://gist.github.com/dharmatech/ded09b46ff700697aef6

How it would look with built-in support for method cascades:

https://gist.github.com/dharmatech/16000fa777356ee74cbc

@emberian
Copy link
Member

It doesn't need to be built-in to use it. The macro can be defined anywhere.

On Mon, May 19, 2014 at 10:50 AM, dharmatech [email protected]:

draw_push_clip as it currently is:

https://gist.github.com/dharmatech/ded09b46ff700697aef6

How it would look with built-in support for method cascades:

https://gist.github.com/dharmatech/16000fa777356ee74cbc


Reply to this email directly or view it on GitHubhttps://github.com//issues/6679#issuecomment-43535114
.

http://octayn.net/

@dharmatech
Copy link
Author

rust-http - handle_request - after

q3 - render - after

@schmee
Copy link
Contributor

schmee commented May 20, 2014

@dharmatech That draw_push_clip example looks really, really nice. More readable, less noise.

@bgdncz
Copy link

bgdncz commented Apr 15, 2015

Any news on this?

@mmstick
Copy link

mmstick commented Jan 16, 2019

Method cascades can currently be done with the cascade crate. Highly critical if you're working with certain types of APIs, such as constructing GTK widgets in gtk-rs. System76 uses it in all of their GTK applications.

flip1995 pushed a commit to flip1995/rust that referenced this issue Feb 11, 2021
Use absolute path to Rust repo in ra_setup

This will convert the path to the Rust repo to an absolute path. This is
important for the clippy_lints/Cargo.toml file. Otherwise if a relative
path is passed, rst-analyzer won't find the Rust repo, because it starts
the relative path search from the clippy_lints dir, not the
rust-clippy dir where the ra_setup command was run from.

changelog: none
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants