3 ms·
Piston is pretty quirky, honestly. The window builder is pretty vanilla, but the event-processing-for-loop stuff is super quirky. The `e` that is yielded by it
by Gankro 11y ago
Piston is pretty quirky, honestly. The window builder is pretty vanilla, but the event-processing-for-loop stuff is super quirky.
The `e` that is yielded by iterating the window is the window itself WHAT
Especially because the examples use stuff that is basically impossible to find in the docs (even though the docs are auto-generated from the source!). The modularity makes it really hard to tell how anything works, what anything is, or what functionality anything has.
- Retra 11y agoYou can also iterate over an `Option` to get its contents: let s = Some(10u32); for val in s { println!("{}", val); } So it's not a completely foreign concept... but maybe they went too far with it?
- Gankro 11y agoYeah both are examples of the designer being a bit "cute". The piston thing is however totally idiomatic and recommended by the piston devs, while I've never seen anyone actually loop over an Option in real code. Though it wouldn't be super terrible if it was idiomatic. I personally prefer to do `s.map(|val| { ... }), but there's a contingent that argues that `map` shouldn't be used purely for side-effects, and should instead be used for, you know, mapping. We've oft argued that if you want to consume an iterator, it aught to be with a `for` loop, so it might make sense to say the same about an Option? Option is such a trivial and core type that you basically end up with 4 ways to do everything you could think of, because semantically distinct conventions all end up doing the exact same thing: for x in data { println!("{}", x) } if let Some(x) = data { println!("{}", x) } data.map(|x| { println!("{}", x) }); match data { Some(x) => println!("{}", x), _ => () } The fact that you can loop over an option is kind of a side-effect of a lot of our APIs taking "thing that is iterable", and evidently it was convenient to provide that for Option (which is after all just a really degenerate collection). You can of course `match` and `if let` an Option because it's an enum, but because there's two cases and one has no state, they end up being completely equivalent (the `_ =>` branch is just an `else`). Then finally you can `map` over an Option because... It's Option. That's what you do with optional types, dangit!