4 ms·
Basically no. The closest thing in rust is traits. But then this is like saying that Haskell basically has ocaml functors (functions from modules to modules) as
by dan-robertson 8y ago
Basically no. The closest thing in rust is traits. But then this is like saying that Haskell basically has ocaml functors (functions from modules to modules) as typeclasses which isn’t true.
Some of what functors do can be done with typeclasses/traits. Eg a functor might take a module with a type and compare function and produce a type of maps fron that type. Meanwhile in Haskell/Rust you get the compare function from a typeclass/trait.
On the other hand you can only have one trait/typeclass implementation per type which requires newtype wrapper awkwardness if you want an alternative. Another problem is that traits tend to be basically small but functors can be big. Eg let’s say you want to write an application for transferring files over various protocols. You might implement a module per protocol which would include things like the type for the configuration, how to read the config, how to make a connection, how to pool connections, how to request a file, how to get chunks of it and so on. These things may not fit so well into a nice typeclass and some Haskell people certainly don’t like typeclasses that don’t correspond to mathematical things. In ocaml the file transferring program might be based on a fuynctor applied to each protocol.
A second thing rust/Haskell don’t have is a way to make a typeclass at runtime whereas one can do that in ocaml, e.g. it’s basically impossible to make a safe type for arithmetic mod n in Haskell/rust if n must be known at runtime. It is actually possible with Reflect in Haskell but that is horrific. It would be nice if one could do something like:
withMod : Integral a => a -> (forall b. (Integral b, Injects a b) => b -> c) -> c
withMod (m:a) f = f (Modm m) where
newtype Modm = Modm a
instance Num Modm where ...
instance Integral Modm where ...
instance Injects a Modm where
inject = Modm
But you can’t. On the other hand not having any way to implicitly know how to serialise or compare things in ocaml is sad.
- smt88 8y agoThis was really interesting and helpful. Thank you for taking the time to type it all out. You inspired me to learn OCaml because I've never thought of it as a language that's capable of expressing things that Haskell just can't.
- chillee 8y agoSome other type features that OCaml has that Haskell doesn't are polymorphic variants and structural polymorphism (row types).