3 ms·
Rust typeclasses solve this problem; you can create a suite of methods with syntax like this: impl methods<T> for [T] { fn filter(f : fn(T)->bool)
by pcwalton 15y ago
Rust typeclasses solve this problem; you can create a suite of methods with syntax like this:
impl methods<T> for [T] {
fn filter(f : fn(T)->bool) -> [T] { ... }
fn map<U>(f : fn(T)->U) -> [U] { ... }
}
And then you can call it with syntax like:
println(#fmt("%?", [ 1, 2, 3 ].map { |x| x + 3 }));
// prints "[ 4, 5, 6 ]"
The methods are properly scoped, so code that isn't in your module needs to import your methods to use them. That way, you avoid introducing strange action-at-a-distance in your code.