2 ms·
I don’t know anything about OCaml so please forgive me if this question seems silly. two sets of arithmetic operators for integers and floats What if I want t
by chongli 7y ago
I don’t know anything about OCaml so please forgive me if this question seems silly.
two sets of arithmetic operators for integers and floats
What if I want to create more numeric types? Say vectors in R^n, for example. Do I need to create more operators for vector addition and scalar multiplication? How do I make sure the user can’t add vectors of incompatible dimension?
- atombender 7y agoYes, you need to add new functions. You cannot redefine "+" to work a custom vector type. Similarly, you cannot have a "print" function that works for all sorts of types. OCaml has print_string, print_int, and so on. OCaml does have parametric polymorphism ("generics"). You can write functions which support generic parameters. And you can use modules as "functors" to sort of package up types in a similar way as Haskell typeclasses (e.g. see [1]). [1] https://stackoverflow.com/questions/14934242/whats-the-closest-thing-to-haskells-typeclasses-in-ocaml https://stackoverflow.com/questions/14934242/whats-the-close...