Re:Python: Uncovering the Overlooked Core Functionalities
by josephg on Monday July 24, 2023 @11:58PM
(#36857691)
Or Rust! Everything that implements the Iterator trait gets access to all of Iterator’s goodies, like map, filter, reduce, etc. Implementing iterator just requires adding a single next(&mut self) -> Option<Item> method on your type.
Lifetimes and async are a massive pain in rust. But the trait system is a work of art.
Re:Python: Uncovering the Overlooked Core Functionalities
by kragen on Tuesday July 25, 2023 @09:21AM
(#36861815)
ruby is closer to what i meant because you can't add methods to rust's iterator, can you? but people add stuff to enumerable all the time
Re:Python: Uncovering the Overlooked Core Functionalities
by josephg on Tuesday July 25, 2023 @01:14PM
(#36865740)
You can!
trait MyIterHelpers: Iterator {
fn dance(&self) {
println!("wheee");
}
}
// And tell rust that all Iterators are also MyIterHelpers.
impl<I: Iterator> MyIterHelpers for I {}
The one caveat is that using it in a different context will need a use crate::MyIterHelpers; line, so the namespace isn't polluted.
Re:Python: Uncovering the Overlooked Core Functionalities
by kragen on Tuesday July 25, 2023 @07:03PM
(#36870951)
neat, i didn't know that was possible
Re:Python: Uncovering the Overlooked Core Functionalities
by zelphirkalt on Wednesday July 26, 2023 @07:38AM
(#36876018)
I like Rust's struct + traits approach, because they avoid inheritance and encourage composition. I am sure people have built bad workarounds though to do inheritance anyway.
You mean Ruby? :P
(All Ruby iteratables mixin Enumerable, which is baaaaaaaasically inheritance.)