On Mon, Oct 21, 2013 at 4:10 PM, Brendan Zabarauskas <[email protected]>wrote:
> chain-oriented APIs (methods like `fn frob(self) -> Frob`)
>
>
> What about:
>
> ~~~
> fn frob(self) -> Frob {
> let mut x = self; // not sure if you need `cast::transmute_mut` here
> x.thing = foo();
> x
> }
> ~~~
>
You don't need the `transmute_mut` here, doing `let mut x = self;` works
just fine.
> That would solve the 'copying' problem.
>
> I was actually considering doing this as a way of initialising an object
> in a fluent style:
>
> ~~~
> let perlin = Perlin::from_seed_str("Kittens")
> .with_frequency(2.0)
> .with_octaves(3);
> ~~~
>
I agree, it's great for initialization, I've used this approach too for
https://github.com/erickt/rust-elasticsearch for building up JSON objects.
I could see some logic to us using chaining by default. It has some nice
symmetry with the iterator protocol:
```
fn frob(xs: ~[int]) -> ~[int] {
xs
.push(0)
.push(1)
.push(2)
.move_iter().map(|x| x + 1).collect()
}
```
_______________________________________________
Rust-dev mailing list
[email protected]
https://mail.mozilla.org/listinfo/rust-dev