Skip to content

Method cascades #6679

Closed
Closed

Description

@dharmatech

Consider method cascades for Rust.

Activity

  1. 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.

  2. 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.

  3. 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.

  4. emberian commented on Aug 5, 2013

    @emberian
    Contributor

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

  5. Aatch commented on Mar 23, 2014

    @Aatch
    Contributor
  6. 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.

  7. 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.

  8. emberian commented on May 19, 2014

    @emberian
    Contributor

    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/

  9. emberian commented on May 19, 2014

    @emberian
    Contributor

    (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/

  10. ariasuni commented on May 19, 2014

    @ariasuni
    Contributor

    I don’t link to lines where the use of cascade operator would me more verbose than without it.

    https://github.com/mozilla/rust/blob/master/src/librustc/back/archive.rs#L59
    https://github.com/mozilla/rust/blob/master/src/librustc/back/rpath.rs#L184
    https://github.com/mozilla/rust/blob/master/src/librustc/middle/liveness.rs#L827
    https://github.com/mozilla/rust/blob/master/src/librustc/middle/liveness.rs#L1026
    https://github.com/mozilla/rust/blob/master/src/librustc/middle/liveness.rs#L1374
    https://github.com/mozilla/rust/blob/master/src/librustc/middle/resolve.rs#L961
    https://github.com/mozilla/rust/blob/master/src/librustc/middle/resolve.rs#L1951
    https://github.com/mozilla/rust/blob/master/src/librustc/middle/resolve.rs#L2398
    https://github.com/mozilla/rust/blob/master/src/librustc/front/feature_gate.rs#L372
    https://github.com/mozilla/rust/blob/master/src/librustc/metadata/filesearch.rs#L148
    https://github.com/mozilla/rust/blob/master/src/librustc/metadata/creader.rs#L320
    https://github.com/mozilla/rust/blob/master/src/librustc/metadata/creader.rs#L420
    https://github.com/mozilla/rust/blob/master/src/librustc/metadata/loader.rs#L376

    I guess it might not be useful to use cascade! in every code I quote though.

  11. emberian commented on May 19, 2014

    @emberian
    Contributor

    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/

  12. dharmatech commented on May 19, 2014

    @dharmatech
    Author

    rust-http - handle_request - after

    q3 - render - after

  13. schmee commented on May 20, 2014

    @schmee
    Contributor

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

  14. cuzbog commented on Apr 15, 2015

    @cuzbog

    Any news on this?

  15. 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.

  16. added a commit that references this issue on Feb 11, 2021
  17. added a commit that references this issue on Aug 21, 2026
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

      Issue actions