# Method-cascading and pipe-forward operators proposal

**URL:** <https://internals.rust-lang.org/t/method-cascading-and-pipe-forward-operators-proposal/7384>\
**Category:** language design\
**Created:** [April 24, 2018, 4:37pm UTC](https://internals.rust-lang.org/t/method-cascading-and-pipe-forward-operators-proposal/7384 "2018-04-24T16:37:13Z")\
**Posts on this page:** 20\
**Page:** 3

<div class="post-metadata">

**Author:** ![160R](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/160r/32/9502_2.png) [@160R](https://internals.rust-lang.org/u/160R)\
**Post date:** [May 1, 2018, 4:31pm UTC](https://internals.rust-lang.org/t/method-cascading-and-pipe-forward-operators-proposal/7384/41 "2018-05-01T16:31:16Z")

</div>

> [@H2CO3](#):
>
> As I’ve already explained, EDSLs can be (and are often) implemented in Rust using macros instead, in a strictly more general way, without the need for custom operators.

But problem is that they **must be implemented** around some data structure - my task is to avoid that by providing common operators that may replace need in macros in some cases.  
When EDSLs implemented this way - everybody should learn new language in language; macro is magic otherwise.

Also macros have poor user experience, completion and highlighting on them lags and I'm trying avoid them everywhere when possible.

Less code - less space for problems.

---

<div class="post-metadata">

**Author:** ![birkenfeld](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/birkenfeld/32/1150_2.png) [@birkenfeld](https://internals.rust-lang.org/u/birkenfeld)\
**Post date:** [May 1, 2018, 4:36pm UTC](https://internals.rust-lang.org/t/method-cascading-and-pipe-forward-operators-proposal/7384/42 "2018-05-01T16:36:03Z")

</div>

> [@160R](#):
>
> Syntax for OpenOptions is worst, since it cumbersome and additionaly requires Default implementation; you also creates two OpenOptions instances.

But you realize that's not how you're supposed to use `OpenOptions`, right?

---

<div class="post-metadata">

**Author:** ![H2CO3](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/h2co3/32/2849_2.png) [@H2CO3](https://internals.rust-lang.org/u/H2CO3)\
**Post date:** [May 1, 2018, 5:04pm UTC](https://internals.rust-lang.org/t/method-cascading-and-pipe-forward-operators-proposal/7384/44 "2018-05-01T17:04:51Z")

</div>

> [@160R](#):
>
> Your criticism was wague

In this case it would probably be better to tell me where it was unclear and I could clarify what I meant.

> [@160R](#):
>
> I think that there always will be programmers that feels itself unproductive and uncomfortable (like me) with reading or writing such code:

I feel sorry for them.

> [@160R](#):
>
> Syntax for OpenOptions is worst, since it cumbersome and additionaly requires Default implementation; you also creates two OpenOptions instances.

Implementing `Default` is literally `#[derive(Default)]` in 99.9% of the time. I don't buy that it's a burden. Creating two instances of the struct could be a valid concern, however the "rest" (`..`) pattern is trivially caught by a compiler optimization. (Even if it's not hard-coded into the compiler as a rule at the MIR stage, for example, I'd still expect LLVM to eliminate redundant allocations and copies/moves. Most `Default`-implementing types don't require more than a single stack allocation.)

> [@160R](#):
>
> Every binding here just holds temporary state, and don’t serves any documenting purposes.

The `open_options` binding could be thrown away altogether in the above (hypothetical) code, since a struct literal is a perfectly valid value to call a method on. The other binding does document that the collection is being mutated while it is being sorted. That's exactly the kind of information I don't want to lose or be hidden by magic syntax.

> [@160R](#):
>
> That code does exactly the same that its “fluent” alternative does and nothing more.

Excuse me? It was exactly my point that the "fluent" alternative does exactly same as this code, and thus the "fluent" style is not necessary, as it doesn't add any value.

> [@160R](#):
>
> It sacrifices in ergonomics and gains nothing (except simplified debugging) from that.

What exactly does it sacrifice in terms of ergonomics?

> [@160R](#):
>
> Returning `self` feels like simple hack when proper solution might exist

If it feels simple that's because it's simple. Simpler and more understandable than a second, special kind of function call. It's an entirely "proper" solution, which works perfectly well and doesn't require any additional language features. There's nothing hackish about it.

---

<div class="post-metadata">

**Author:** ![bill\_myers](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/bill_myers/32/4085_2.png) [@bill\_myers](https://internals.rust-lang.org/u/bill_myers)\
**Post date:** [May 2, 2018, 1:21am UTC](https://internals.rust-lang.org/t/method-cascading-and-pipe-forward-operators-proposal/7384/45 "2018-05-02T01:21:24Z")

</div>

The proposed syntax doesn’t really seem intuitive at all, and discarding the result seems a dubious design

Maybe something like this could work:

```rust
hash_map.{
    .insert(a, b);
    if .len() == 3 {
        .insert(c, d);
    }
}

```

But overall doesn’t seem worth it: you can just do “let x = hash\_map” and then only repeat “x” for each invocation:

```rust
{
    let x = &mut hash_map;
    x.insert(a, b);
    if x.len() == 3 {
        x.insert(c, d);
    }
}

```

or with match:

```rust
match hash_map {
    ref mut x => {
        x.insert(a, b);
        if x.len() == 3 {
            x.insert(c, d);
       }
   }
}

```

Could maybe consider some syntax sugar for that, like this:

```rust
with x = hash_map {
    x.insert(a, b);
    if x.len() == 3 {
        x.insert(c, d);
   }
}

```

but that also doesn’t seem worth it.

---

<div class="post-metadata">

**Author:** ![gbutler](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/gbutler/32/3670_2.png) [@gbutler](https://internals.rust-lang.org/u/gbutler)\
**Post date:** [May 2, 2018, 1:33am UTC](https://internals.rust-lang.org/t/method-cascading-and-pipe-forward-operators-proposal/7384/46 "2018-05-02T01:33:28Z")

</div>

> [@bill\_myers](#):
>
> ```rust
> {
> let x = &mut hash_map;
> x.insert(a, b);
> if x.len() == 3 {
> x.insert(c, d);
> }
> }
> 
> ```

I find I do this sort of thing when needed in, for example, Java or C#. I don't see how any of the proposals is much of an improvement on this.

---

<div class="post-metadata">

**Author:** ![bill\_myers](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/bill_myers/32/4085_2.png) [@bill\_myers](https://internals.rust-lang.org/u/bill_myers)\
**Post date:** [May 2, 2018, 12:29pm UTC](https://internals.rust-lang.org/t/method-cascading-and-pipe-forward-operators-proposal/7384/47 "2018-05-02T12:29:23Z")

</div>

I came up with yet another idea that might actually be somewhat reasonable.

Copy the “let x in” syntax from functional programming languages:

```rust
let x = &mut hash_map in {
    x.insert(a, b);
    if x.len() == 3 {
        x.insert(c, d);
    }
}

```

```rust
foo(let x = &mut hash_map in {x.insert(a, b); x.len()})

```

“in” is already a keyword in Rust, and this syntax is already established in Haskell and other languages.

Also it could be seen as a variant of “if let” for non-refutable patterns.

It doesn’t differ much syntactically from just putting the let inside the block though so might not be worth having two ways of doing the same thing.

---

<div class="post-metadata">

**Author:** ![160R](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/160r/32/9502_2.png) [@160R](https://internals.rust-lang.org/u/160R)\
**Post date:** [May 2, 2018, 2:35pm UTC](https://internals.rust-lang.org/t/method-cascading-and-pipe-forward-operators-proposal/7384/48 "2018-05-02T14:35:34Z")

</div>

> [@H2CO3](#):
>
> Implementing Default is literally #[derive(Default)] in 99.9% of the time. I don’t buy that it’s a burden. Creating two instances of the struct could be a valid concern, however the “rest” (..) pattern is trivially caught by a compiler optimization. (Even if it’s not hard-coded into the compiler as a rule at the MIR stage, for example, I’d still expect LLVM to eliminate redundant allocations and copies/moves. Most Default-implementing types don’t require more than a single stack allocation.)

Builders also can be derived in such cases and probably optimized by compiler.  
And code on them is more ergonomically simpler. If `OpenOptions` will be modified and construction will require builder implementation (e.g. `build()` - to perform some validation logic) - you must change it everywhere; that not happens when you uses builder initially.  
Also jumping to builder implementation in IDE is also simpler that finding `Default`.

> [@H2CO3](#):
>
> The open\_options binding could be thrown away altogether in the above (hypothetical) code, since a struct literal is a perfectly valid value to call a method on.

> [@birkenfeld](#):
>
> But you realize that’s not how you’re supposed to use OpenOptions, right?

Right, it might be better

> [@H2CO3](#):
>
> Excuse me? It was exactly my point that the “fluent” alternative does exactly same as this code, and thus the “fluent” style is not necessary, as it doesn’t add any value.

But my point was that "imperative" style is not necessary

> [@H2CO3](#):
>
> What exactly does it sacrifice in terms of ergonomics?

More characters/keywords to read/type, code is not fluent

> [@H2CO3](#):
>
> If it feels simple that’s because it’s simple. Simpler and more understandable than a second, special kind of function call. It’s an entirely “proper” solution, which works perfectly well and doesn’t require any additional language features. There’s nothing hackish about it.

It's "proper" solution because there is no better. It not always works and additionally requires programmer to write boilerplate that is simple enough to be replaced by operator - that's hackish

---

<div class="post-metadata">

**Author:** ![H2CO3](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/h2co3/32/2849_2.png) [@H2CO3](https://internals.rust-lang.org/u/H2CO3)\
**Post date:** [May 2, 2018, 2:58pm UTC](https://internals.rust-lang.org/t/method-cascading-and-pipe-forward-operators-proposal/7384/49 "2018-05-02T14:58:22Z")

</div>

> [@160R](#):
>
> Also jumping to builder implementation in IDE is also simpler that finding Default.

Citation needed.

> [@160R](#):
>
> you must change it everywhere; that not happens when you uses builder initially.

I don't follow this. If you use the rest-syntax with structs from the beginning and you don't change to any other "pattern", you can also make this argument.

> [@160R](#):
>
> But my point was that “imperative” style is not necessary

There's nothing imperative in constructing a value from two other values. In fact, it's a pure operation. (Haskell programs do it all the time.)

> [@160R](#):
>
> More characters/keywords to read/type

Typing just doesn't matter. And in terms of reading, it's usually not the _amount_ of code that matters, but the understandability. And 10-something characters less of magic is less understandable than a little bit more non-magic code.

> [@160R](#):
>
> It not always works and additionally requires programmer to write boilerplate that is simple enough to be replaced by operator

There's not a single piece of boilerplate in it that I could identify, just like a case when it "doesn't work".

---

<div class="post-metadata">

**Author:** ![H2CO3](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/h2co3/32/2849_2.png) [@H2CO3](https://internals.rust-lang.org/u/H2CO3)\
**Post date:** [May 2, 2018, 2:58pm UTC](https://internals.rust-lang.org/t/method-cascading-and-pipe-forward-operators-proposal/7384/50 "2018-05-02T14:58:56Z")

</div>

> [@bill\_myers](#):
>
> It doesn’t differ much syntactically from just putting the let inside the block though so might not be worth having two ways of doing the same thing.

Exactly my thoughts.

---

<div class="post-metadata">

**Author:** ![160R](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/160r/32/9502_2.png) [@160R](https://internals.rust-lang.org/u/160R)\
**Post date:** [May 2, 2018, 4:06pm UTC](https://internals.rust-lang.org/t/method-cascading-and-pipe-forward-operators-proposal/7384/51 "2018-05-02T16:06:23Z")

</div>

# Second version

**Completely different than previous** , features:

- Revealing of hidden mutations inside of method call chains
- Flexible syntax for _method cascading_
- Redesigned _pipe-forwarding_
- EDSLs

# 1. Explicit mutations on method call chains

Purpose of that is to make regular functions call consistent with features that will be introduced in next sections, and to prevent temporary mutable bindings to stay accessible in whole scope.

It forces functions taking `&mut self` to prepend `~` when invoked.  
**Don’t confuse that with previous `~`** - it’s different and doesn’t change flow but acts more like annotation on method call.

Syntax is:

- `mutable.~function()` - to indicate possible mutation on `mutable` value

where `mutable` expresses mut binding or function call chain. That’s to not write it twice and the same convention is applied further in proposal.

Using `~` on functionts that mutates frees us from introducing temporary mut bindings inside of method call chain, because it already shows all relevant information:

- Which functions does mutations and on what value
- Where _anonymous mutable bindings_ are introduced

I think that `~` is good sigil here, since it’s short, easy to remember and type, also is consistent with features that will be introduced in next sections, and looks like creased dash which brings some associations with mutation.  
Yes, it’s another operator and programmers must learn it. However, reading TRPL is mandatory for every rustacean, so once operator will be defined and documented - nobody will be confused.

Code will look like this:

```rust
    let mut collection = get_collection(); // `mut` is still required here.
    collection.~sort(); // `~` shows where we do mutation.

```

```rust
    return Type::new() // Anonymous mutable binding introduced here.
        .~mutate1() // This function mutates return value of `new`.
        .~mutate2() // And this mutates return value of `mutate1`.
        .transform(); // But this don't mutates anything.

```

_Also, that’s breaking change_.  
Is it worth it - for discussion (but read whole proposal before disclaiming).

# 2. Side-effects on method call chain

That’s supposed to be alternative to _method cascading_.  
It allows to apply batch of functions that takes `self`, `&self` or `&mut self` to value without breaking chain.

Syntax is:

- `value.>(function(,),)` - to call one or multiple `function`s on `value` and ignore result of them
- `mutable.~>(function(,),)` - if one or more of `function`s needs value to be bound as `mut`

where trailing comma means that other functions/arguments might be applied.

Let analyze all parts:

1. `~` has the same meaning as in previous section - function mutates. Also nicely combines with `>` into `~>` arrow.
2. `~>` and `>` neatly shows direction where `value` is passed.
3. `.` is required to show that method call and (possible) (de)referencing occurs. It also fills space for proper alignment and keeps chain syntax consistent (I loathe how e.g. `|>` breaks chain visually).
4. `(` and `)` shows that we have limited scope where we can operate on value.
5. `(` and `)` are chosen over `{` and `}` to not confuse that syntax with expressions and to not add new symbols in call chain syntax.
6. `,` separated functions - it’s to group method calls to not search through chain of `.~>`/`.>`

It implies that `function` takes `self` by reference or `Clone` is implemented for it, since you can’t continue chain or apply other side-effects if subject value was moved somewhere.

How it looks on practice:

```rust
    let hmap = HashMap::new().~> ( // Anonymous mut bining introduced by `.~>`.
        insert("key1", val1), // `insert` is called on `mut HashMap`.
        insert("key2", val2), // Also is called on `mut HashMap`.
    ); // HashMap is returned from parentesis.

```

```rust
    let mut collection = get_collection(); // `mut` is required on binding.
    collection.~>(sort()); // `.~>` takes `mut collection` into `sort`.
        .iter()… // We can continue chain on `mut collection`.

```

```rust
    return OpenOptions::new()
        .~>(write(true), read(true)) // Single-line representation.
        .open("location")?
        .to_some_collection()
        .~>(sort()); // Single side-effect method called.

```

```rust
    let value = get_value()
        .> (action_1(), // Value can't be taken as `&mut self` here.
            action_2()) // Proper aligning.

```

Don’t confuse it with various `withers`, since it don’t allows to run arbitrary expressions with implicit context.  
Only methods chains - nothing more.

# 3. Calling external functions on value

That’s supposed to be alternative to _pipe forwarding_.  
Here I will start with reasons that shows great need behind it:

1. Functional programmers uses it extensively, modern languages have it in arsenal, it helps, and not providing it in Rust is rather restrictive.
2. Dense usage of meaningless bindings, `let`, `mut`, `;` in code, necessity of imperative style - here is reason why Rust is considered as harsh and verbose language, not abundance of operators.
3. _Side-effects on method call chain_ will be allowed to apply external functions on subject value. **This is very important** to keep code modular and to make EDSLs introduced in next section extensible.
4. Declarative programming style is less verbose and more descriptive, thus safer - that’s why Rust should adopt and promote it.

Syntax is:

- `value.function(,in,)` - to pass `value` into `function`. Use `&in` to pass by reference
- `mutable.~function(,&mut in,)` - to pass `mutable` into `function` by mut reference
- `value.> (,side_effect(,in,),)` - to pass `value` into `side_effect`. Use `&in` to pass by reference
- `mutable.~>(,side_effect(,&mut in,),)` - to pass `mutable` into `side_effect` by mut reference

where trailing commas means that other arguments/functions might be added.

It don’t differ too much from regular function call.  
But that’s not very important to know, which kind of function we call: associated or external, so visibility is sufficient (and it’s actually good when function has one argument).

`in` is choosed because it’s short, descriptive, already used as keyword, and has syntax highlighting.  
There can be other placeholder, e.g. `it`, `this`, `that` - decision might be changed.

Examples:

```rust
    return collection.iter()
        .apply_mapping_combinators(in) // Iterator moved to `apply_mapping_…`
        .apply_logging_combinators(in) // Iterator moved to `apply_logging_…`
        .collect()

```

```rust
    return Type::builder()
        .~apply_common_settings(&mut in) // To mutate builder.
        .build()

```

```rust
    long_name_binding
        .borrowed(&in, &in, &in) // This is allowed for all types.
    long_name_binding
        .copied(in, in, in) // This is allowed only if `Copy` is implemented.

```

```rust
    let text = String::new()              
        .~>(put_default_text(&mut in),
            ecranize_shell(&mut in))
        .> (debug(&in),
            send(&in));

```

* * *

###### Experimental

Support for macros:

```rust
    get_value()
        .start_chain()
        .>(println!("start: {}", &in)) // Macros treaten as regular functions.
        .continue_chain()
        .println!("continue: {}", &in);

```

Support for constructors:

```rust
    get_value()
        .take_enum(Some(in))
        .take_struct(Struct { x: in, y: { in_is_not_visible_here } });

```

# 4. Splitted chain

This section is most interesting in whole proposal and has best examples.  
It’s idea is simply to allow to call functions further on _side-effects_ results.

Usecases are:

1. Error handling from external functions: mapping, unwrapping, using `?`
2. EDSLs or _Kotlin-like type-safe builders_: without returning `self`, lambdas, scoping issues, prepending `~` on each `&mut self`-taking function, implicitness, and additional boilerplate around

Examples:

```rust
    return PathBuf::from("base")
        .>(fs::create_dir_all(&in).unwrap()) // Here we actually splitted chain.
        .~push("filename") // Don't forget - it mutates.
        .File::open(in);

```

```rust
    let content = String::new()
        .~>(file.read_to_string(&mut in)?); // `?` is applied to read_to_string

```

```rust
    return Tree::root(0).~> ( // `top-level` started.
        branch(1), // Added `branch1` to `top-level`.
        branch(1).~> ( // Other `branch1` added to `top-level`.
            branch(2), // Added `branch2` to `branch1`.
            branch(2), // ...
            branch(2).~> (
                branch(3),
                branch(3),
            ),
            branch(2),
            add_other_branches(in), // External function is called on branch
        )
    );

```

```rust
    let matches = App::new("My Super Program").~> ( // Prototype from clap's README
        version("1.0"),
        author("Kevin K. <kbknapp@gmail.com>"),
        about("Does awesome things"),
        arg("config").~> (
            short("c"),
            value_name("FILE"),
            help("Sets a custom config file"),
            takes_value(true),
        ),
        arg("INPUT").~> (
            help("Sets the input file to use"),
            required(true),
            index(1),
        ),
        subcommand("test").~> (
            about("controls testing features"),
            author("Someone E. <someone_else@other.com>"),
            arg("debug").~> (
               short("d"),
               help("print debug information verbosely"),
            ),
        ),
    ).get_matches();

```

* * *

# Summary

1. Don’t looks that bad
2. Introduces ~~three~~ **edit** : two operators, however their usage is very intuitive
3. Fixes some existed problems and reduces boilerplate
4. Introduces breaking change
5. Promotes different programming style

---

<div class="post-metadata">

**Author:** ![H2CO3](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/h2co3/32/2849_2.png) [@H2CO3](https://internals.rust-lang.org/u/H2CO3)\
**Post date:** [May 2, 2018, 8:47pm UTC](https://internals.rust-lang.org/t/method-cascading-and-pipe-forward-operators-proposal/7384/52 "2018-05-02T20:47:59Z")

</div>

It doesn’t address the issue of having to memorize, understand, distinguish between, and correctly use several different forms of method/functions calls, though. I don’t really want to see at least three (or four if we count the trailing closure syntax) different forms for a function call. It’s more distracting than clarifying.

---

<div class="post-metadata">

**Author:** ![derekdreery](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/derekdreery/32/2632_2.png) [@derekdreery](https://internals.rust-lang.org/u/derekdreery)\
**Post date:** [May 2, 2018, 10:03pm UTC](https://internals.rust-lang.org/t/method-cascading-and-pipe-forward-operators-proposal/7384/53 "2018-05-02T22:03:36Z")

</div>

I love Haskell syntax, but I’m not sure it’s right for Rust. I do really like the boxing-unboxing TypeClasses like functors, monads et. al., and it would be fun to explore how they would apply to Rust, and whether new operators would help.

I also think that the changes to the compiler (the query pattern) would make it pretty easy to experiment with different syntax whilst still using the same typeck, borrowck etc. I think it would be really fun to try to create a ML-style syntax for Rust, not saying it would ever get endorsed by the rust lang devs tho 😛

---

<div class="post-metadata">

**Author:** ![derekdreery](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/derekdreery/32/2632_2.png) [@derekdreery](https://internals.rust-lang.org/u/derekdreery)\
**Post date:** [May 2, 2018, 10:04pm UTC](https://internals.rust-lang.org/t/method-cascading-and-pipe-forward-operators-proposal/7384/54 "2018-05-02T22:04:39Z")

</div>

Side note: I love how anyone can come along and suggest changes to the language, and they will get taken seriously by the core team. Don’t get that with C#! 😛

---

<div class="post-metadata">

**Author:** ![Centril](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/centril/32/3334_2.png) [@Centril](https://internals.rust-lang.org/u/Centril)\
**Post date:** [May 2, 2018, 10:06pm UTC](https://internals.rust-lang.org/t/method-cascading-and-pipe-forward-operators-proposal/7384/55 "2018-05-02T22:06:54Z")

</div>

> [@derekdreery](#):
>
> the core team

Side-side-note: It's actually the [lang team](https://www.rust-lang.org/en-US/team.html) 😉

---

<div class="post-metadata">

**Author:** ![160R](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/160r/32/9502_2.png) [@160R](https://internals.rust-lang.org/u/160R)\
**Post date:** [May 3, 2018, 11:16am UTC](https://internals.rust-lang.org/t/method-cascading-and-pipe-forward-operators-proposal/7384/56 "2018-05-03T11:16:35Z")

</div>

> [@H2CO3](#):
>
> It doesn’t address the issue of having to memorize, understand, distinguish between, and correctly use several different forms of method/functions calls, though

Ok, I found that some parts are really missing and updated second proposal, also added better examples.  
Does that clarified something?

---

<div class="post-metadata">

**Author:** ![MajorBreakfast](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/majorbreakfast/32/4015_2.png) [@MajorBreakfast](https://internals.rust-lang.org/u/MajorBreakfast)\
**Post date:** [May 3, 2018, 12:18pm UTC](https://internals.rust-lang.org/t/method-cascading-and-pipe-forward-operators-proposal/7384/57 "2018-05-03T12:18:51Z")

</div>

I appreciate the effort you’re putting into this. That said I don’t like the proposed operators and here’s why:

- This calling style is most useful for places where the builder pattern is used. However, the builder pattern can already be implemented with the tools that the language offers today. And it looks great!
- The resulting code is only marginally more compact.
- It’s unnecessarily complicated:
  - Three (!) new operators: `~`, `~>` and `->`. Too much magic punctuation can be a real pain.
  - `let` bindings can already do this. The creation of a temporary binding with a short name is easy and the code is only slightly longer.
  - I imagine that people who are not using Rust everyday and are not familiar with every part of the language will groan when they encounter these operators. They’ll immediately get a feeling for what the code is probably doing, but they also see that there is some weird syntax going on which they need to learn to fully understand the code.
  - Status quo is better: Currently there are just function calls and even they have some hidden details (Deref) that one has to be aware of. This system introduces more details. You can only keep so much in your head while coding. I think keeping things nice and simple is better for beginners and experts.

---

<div class="post-metadata">

**Author:** ![160R](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/160r/32/9502_2.png) [@160R](https://internals.rust-lang.org/u/160R)\
**Post date:** [May 7, 2018, 12:21pm UTC](https://internals.rust-lang.org/t/method-cascading-and-pipe-forward-operators-proposal/7384/58 "2018-05-07T12:21:16Z")

</div>

You provided great summary from point of view of person who already involved in Rust and knows all nuances.  
I still feel that proposal is important because I had different experience.  
There is answer to 3 quotes:

> [@MajorBreakfast](#):
>
> This calling style is most useful for places where the builder pattern is used. However, the builder pattern can already be implemented with the tools that the language offers today. And it looks great!

> [@MajorBreakfast](#):
>
> I imagine that people who are not using Rust everyday and are not familiar with every part of the language will groan when they encounter these operators. They’ll immediately get a feeling for what the code is probably doing, but they also see that there is some weird syntax going on which they need to learn to fully understand the code.

> [@MajorBreakfast](#):
>
> Status quo is better: Currently there are just function calls and even they have some hidden details (Deref) that one has to be aware of. This system introduces more details. You can only keep so much in your head while coding. I think keeping things nice and simple is better for beginners and experts.

Rust is great language that is pleasure to work with.  
Probably little misunderstanding or my idealistic style of thinking might influence that. But for me status quo syntax was magic and rather confusing when learning.  
And here's story about how I come to this proposal:

It begins with my first steps with Rust when I've just learn that all mutations should be explicit and compiler will take care of it. I said to myself:  
"Awesome! But probably because of that Rust code is pretty verbose. Interesting, how looks builder pattern usage?".  
When I checked, I was surprised that it looking exactly the same as in other languages.  
"Hmm... Probably some complex borrow-checking magic here... Or it's cloned everytime. It's better to accept it as it is for now".

And that was fine, until... I've written method with single pretty iterator chain which turns into `collect()`, then I realized that it'll help me with debugging if `Vec` returned from that method will be sorted.  
So, I decided to temporarily add `sort()` and was surprised: "Why it don't returns `self`? Now I must add `let mut collection`, `collection.sort()`, `collection` in different places and again remove all that. Never expected!"

I've started googling "why Rust sort don't returns self" and found Github issue which explains that it's to make mutations in method chains explicit.  
That was fun enough and another question arrived: "So, if method returns `self` then mutation in it might be hidden?"  
Well... I've convinced after experimenting, and felt deceived and disappointed.

* * *

I can deal with that thoughts, but might someone else experience the same?  
If there will be better syntax it'll be not confused 3 times before understanding and there will be some gratification from possibilities.

> [@MajorBreakfast](#):
>
> The resulting code is only marginally more compact.

> [@MajorBreakfast](#):
>
> let bindings can already do this. The creation of a temporary binding with a short name is easy and the code is only slightly longer.

As you see intention was not only to make it more compact but also to make it explicit and less confusing/restrictive.  
It's more like writing `?` instead of `try!`. Only marginally compact - it in many ways better experience, since you read/type actual code and not language items.

> [@MajorBreakfast](#):
>
> Three (!) new operators: ~, ~\> and -\>. Too much magic punctuation can be a real pain.

That's true and that's definitely reason to not add it, but also reason to experimenting further.

I've just realized that _new operators count might be reduced to two_: `.->` was choosed because it looks "nice" and without other meaningful reasons.  
It might be simplified to `.>` and that will be fine - when you need to ignore result of function you uses `.>` and when you need to ignore result function that mutates you uses `.~>`.  
That's more intuitive and there will be no problem with distinguishing between two forms.

@Centril proposed very similar syntax in [this post](https://internals.rust-lang.org/t/method-cascading-and-pipe-forward-operators-proposal/7384/35) and I was inspired by it.

I've edited [my post](https://internals.rust-lang.org/t/method-cascading-and-pipe-forward-operators-proposal/7384/51) where replaced `.->` with `.>`

---

<div class="post-metadata">

**Author:** ![MajorBreakfast](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/majorbreakfast/32/4015_2.png) [@MajorBreakfast](https://internals.rust-lang.org/u/MajorBreakfast)\
**Post date:** [May 7, 2018, 1:46pm UTC](https://internals.rust-lang.org/t/method-cascading-and-pipe-forward-operators-proposal/7384/59 "2018-05-07T13:46:46Z")

</div>

> [@160R](#):
>
> That’s true and that’s definitely reason to not add it, but also reason to experimenting further.

True. Who knows, maybe there is a good syntax out there. However, currently I'm very skeptical about this feature. The example where you transformed clap's API to use this syntax looks less clear than it does without. That's unfortunately not reassuring.

About collecting into a sorted vector, [here's a recently opened thread](https://internals.rust-lang.org/t/pre-rfc-ish-add-vec-sorted-and-friends/7454) by @matklad.

Also I've found [this crate](https://github.com/colin-kiegel/rust-derive-builder) that makes implementing builder patterns easy.

---

<div class="post-metadata">

**Author:** ![160R](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/160r/32/9502_2.png) [@160R](https://internals.rust-lang.org/u/160R)\
**Post date:** [May 21, 2018, 5:08am UTC](https://internals.rust-lang.org/t/method-cascading-and-pipe-forward-operators-proposal/7384/60 "2018-05-21T05:08:18Z")

</div>

I simplified syntax a bit.

Here functions that takes `&mut self` just called with `~` instead of `.`

```rust
    let mut collection = get_collection(); // `mut` is still required here.
    collection~sort(); // `~` shows where we do mutation.

```

```rust
    return Type::new()  
        ~mutate1() // This function mutates return value of `Type::new`. 
        ~mutate2() // And this mutates return value of `mutate1`.  
        .transform(); // This don't mutates anything.             

```

Side-effects syntax also was changed to `.{ fn_call; ... }` and `~{ mut_fn_call; ... }`

```rust
    let hmap = HashMap::new()~ {
        insert("key1", val1); // `insert` is called on `mut HashMap`.
        insert("key2", val2); // This also is called on `mut HashMap`.
    };

```

```rust
    return OpenOptions::new()
        ~{read(true); write(true);} // Single-line representation     
        .open("location")?;     

```

```rust
    let mut collection = get_collection(); // `mut` is still required on binding.
    collection
        ~{sort();}
        .for_each(action);

```

```rust
    fn draw_scene(drawing: DrawApi) -> DrawApi {
        drawing. { 
            set_color(Color::Red);
            draw_circle(Circle::defined());
            draw_line(0, 0, 100, 100);
            draw_rectangle(specific_regtangle());
        }
    }

```

```rust
    fn set_abc(&mut self, (a, b, c): Tuple) -> Result<(), Error> {
        self.inner.{ set_a(a)?; set_b(b)?; set_c(c)?; };
        Ok(())
    }

```

In syntax for calling non-associated functions `in` is changed to `it`. That will be more intuitive

```rust
    return collection.iter()
        .apply_mapping_combinators(it) // Iterator moved to `apply_mapping_…`
        .apply_logging_combinators(it) // Iterator moved to `apply_logging_…`
        .collect();

```

```rust
    return x.sum(it).abs(it).sqrt(it).round(it, 2);

```

```rust
    return Type::builder()
        ~apply_common_settings(&mut it) // To mutate builder.
        .build()

```

```rust
    long_name_binding
        .borrowed(&it, &it, &it); // Multiple borrowing is allowed for all types.

    long_name_binding
        .copied(it, it, it); // Copying is allowed only if `Copy` is implemented.

```

```rust
    let message = new_message(). {
        send_a(&it);
        send_b(&it);
    };

```

```rust
    let text = String::new()~ {
        put_default_text(&mut it);
        put_additional_text(&mut it);
        debug_text(&it);
    };

```

And chain splitting will look like on following examples

```rust
    return PathBuf::from("base")
        .{fs::create_dir_all(&it).unwrap();} 
        ~push("filename")   
        .File::open(it);

```

```rust
    let content = String::new()
        ~{file.read_to_string(&mut it)?;};

```

```rust
    return Tree::root(0)~ { // `top-level` started.
        branch(1); // Added `branch1` to `top-level`.
        branch(1)~ { // Other `branch1` added to `top-level`.
            branch(2); // Added `branch2` to `branch1`.
            branch(2); // ...
            branch(2)~ {
                branch(3);
                branch(3);
            },
            branch(2);
            add_other_branches(&mut it); // External function is applied
        },
    };

```

```rust
    let matches = App::new("My Super Program")~ {
        version("1.0");
        author("Kevin K. <kbknapp@gmail.com>");
        about("Does awesome things");
        arg("config")~ {
            short("c");
            value_name("FILE");
            help("Sets a custom config file");
            takes_value(true);
        };
        arg("INPUT")~ {
            help("Sets the input file to use");
            required(true);
            index(1);
        };
        add_other_args(&mut it);
        subcommand("test")~ {
            about("controls testing features");
            author("Someone E. <someone_else@other.com>");
            arg("debug")~ {
                short("d");
                help("print debug information verbosely");
            };
        };
    }.get_matches();

```

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [March 25, 2019, 8:30am UTC](https://internals.rust-lang.org/t/method-cascading-and-pipe-forward-operators-proposal/7384/61 "2019-03-25T08:30:07Z")

</div>

Closed on March 25, 2019, 8:30am UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.

[Previous page](https://internals.rust-lang.org/t/method-cascading-and-pipe-forward-operators-proposal/7384.md?page=2)
