4 ms·
The syntax feels a bit Ruby-ish, doesn't it? Here's an example from the article of how they could expand the syntax in the future: foo.bar(..).baz(..).match
by mklein994 7y ago
The syntax feels a bit Ruby-ish, doesn't it? Here's an example from the article of how they could expand the syntax in the future:
foo.bar(..).baz(..).match {
Variant1 => { ... }
Variant2(quux) => { ... }
}
Compared to some Ruby:
5.times {
puts "Hello world!"
}
Note I'm by no means an expert in Ruby or Rust, this is just what I thought of when I saw the syntax.
- hombre_fatal 7y agoA .match "method" just opens the dam to "why stop there?" I prefer Kotlin's general approach of .let and similar: number = foo.bar().baz().let { match it { A => 1 B => 2 } } Now anyone has the general tool for chaining without needing library authors or language designers to create the API for them.
- jnetterf 7y agoNeat! When working with Options/Results/Iterators, you can use `.map` [1] for exactly this purpose. It would sometimes be convenient to have something like `.map` / `.let` on unwrapped values as well. [1] https://doc.rust-lang.org/std/option/enum.Option.html#method.map https://doc.rust-lang.org/std/option/enum.Option.html#method..., https://doc.rust-lang.org/std/result/enum.Result.html#method.map https://doc.rust-lang.org/std/result/enum.Result.html#method..., https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.map https://doc.rust-lang.org/std/iter/trait.Iterator.html#metho...
- centril 7y agoNote that in Rust, `.match` would itself provide the general way: let number = foo.bar().baz().match { it => it + 1 };
- pitterpatter 7y agoRe: your second example, once upon a time you could write something very similar in Rust: do 5.times { // ... } Where do was just sugar for calling functions that took a closure as the last argument: fn foo(a: A, ..., z: Z, cl: ||) { ... } foo(a, ..., z, || { ... }) do foo(a, ..., z) { ... }
- pitterpatter 7y agoIn fact, before the current for ... in ... syntax, this is how foreach looping used to be done: do some_array.each |item| { ... }