3 ms·
Haskell's polymorphism isn't the same as rust. Rust has two kinds of polymorphism. Static dispatch: fn show<T>(a: T) -> String where T: Display { Di
by iudqnolq 6y ago
Haskell's polymorphism isn't the same as rust. Rust has two kinds of polymorphism.
Static dispatch:
fn show<T>(a: T) -> String
where T: Display
{
Display::fmt(a)
}
fn main() {
show(1_i32);
show(1.0_f32);
}
is (handwaves) first translated to a version with no generics
fn show_i32(a: i32) -> String {
i32::fmt(a)
}
fn show_f32(a: f32) -> String {
f32::fmt(a)
}
fn main() {
show_i32(1_i32);
show_f32(1.0_f32);
}
You can probably see how the more permutations of functions you need to generate the slower it gets.
Dynamic dispatch:
fn show(a: Box<dyn Display>) -> String {
Display::fmt(a)
}
fn main() {
show(1_i32);
show(1.0_f32);
}
This translates to (handwaves):
fn show(a: Box<dyn Display>) -> String {
let fmt_fn = lookup_fn(Display::fmt, typeid_of(a))
fmt_fn(a)
}
fn main() {
show(1_i32);
show(1.0_f32);
}
This is simple to compile, but you need to do some extra work at runtime.
- gpderetta 6y agoThat's not an answer though. I believe (but I know little of both Haskell and Rust) that Haskell polymorphism is at least as powerful as rust static polymorphism but it is normally implemented via type erasure. I think the real reason is that rust gives additional guarantees about lack of allocation and object layout that are hard to implement with dictionary passing (BitC tried and failed).
- Tyr42 6y agoI agree somewhat, in Haskell you can write generic functions without traits, and I think they are not monomorphized. (map for example.) However, I think these all operate on boxed objects, which is a little bit easier to do than for Rust, where only some objects are boxed. In short, I agree.