3 ms·
> it would certainly be interesting to program in a language ... where all iterables had to derive from a common base class, but it would be a very different l
by hwayne 3y ago
> it would certainly be interesting to program in a language ... where all iterables had to derive from a common base class, but it would be a very different language from python
You mean Ruby? :P
(All Ruby iteratables mixin Enumerable, which is baaaaaaaasically inheritance.)
- josephg 3y agoOr 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.
- kragen 3y agoruby 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
- josephg 3y agoYou 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.
- kragen 3y agoneat, i didn't know that was possible
- zelphirkalt 3y agoI 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.
- pjmlp 3y agoOr its inspiration, Smalltalk.