Skip to content

Method cascades #6679

@dharmatech

Description

@dharmatech

Consider method cascades for Rust.

Activity

mstewartgallus

mstewartgallus commented on May 22, 2013

@mstewartgallus
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

metajack commented on May 22, 2013

@metajack
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

bstrie commented on May 22, 2013

@bstrie
Contributor

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

emberian commented on Aug 5, 2013

@emberian
Member

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

Aatch

Aatch commented on Mar 23, 2014

@Aatch
Contributor

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

ariasuni

ariasuni commented on May 18, 2014

@ariasuni
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

bstrie commented on May 19, 2014

@bstrie
Contributor

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

emberian commented on May 19, 2014

@emberian
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 notifications@github.comwrote:

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

emberian commented on May 19, 2014

@emberian
Member

(Especially since we have functional record update)

On Sun, May 18, 2014 at 7:35 PM, Corey Richardson corey@octayn.net 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 notifications@github.comwrote:

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

dharmatech commented on May 19, 2014

@dharmatech
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

emberian commented on May 19, 2014

@emberian
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 notifications@github.comwrote:

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

dharmatech commented on May 19, 2014

@dharmatech
Author

rust-http - handle_request - after

q3 - render - after

schmee

schmee commented on May 20, 2014

@schmee
Contributor

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

cuzbog

cuzbog commented on Apr 15, 2015

@cuzbog

Any news on this?

mmstick

mmstick commented on Jan 16, 2019

@mmstick

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.

added a commit that references this issue on Feb 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @dharmatech@metajack@Aatch@emberian@bstrie

        Issue actions

          Method cascades · Issue #6679 · rust-lang/rust