4 ms·
F# has operator overloading, which is missing from ocaml?
by rienbdj 7y ago
F# has operator overloading, which is missing from ocaml?
- phillipcarter 7y agoI'm not sure if I'd phrase it in terms of something missing from OCAML (or F# for that matter). There's a substantial diff between OCAML and F# at this point, once you get past "core" programming with functions in both languages. Some of the more prominent things F# has that * Computation Expressions * Perf abstractions and compiler analysis for them, namely Span<'T> * Type Providers * The .NET generics system * Anonymous Records * Slices and ranges for slicing or generating list-like data And as the parent user said, there's plenty OCAML offers that is missing from F#. I think it's a good idea to consider them quite differently, despite having the same functional core.
- yawaramin 7y agoIt's interesting to compare some of those things, though. * OCaml has now landed monadic and applicative binding syntax ( http://jobjo.github.io/2019/04/24/ocaml-has-some-new-shiny-syntax.html http://jobjo.github.io/2019/04/24/ocaml-has-some-new-shiny-s... ), which I think makes it a superset of F# computation expressions. * Regarding Span<'T> (or my preference 'T Span :-), I believe F# and OCaml have developed different optimizations and constraints. For example, in OCaml allocation is really cheap. But there are still well-known techniques for minimizing it. * OCaml also has the PPX system, which allows to programmatically transform a program's syntax tree into a new tree. I believe this allows somewhere around the same power as type providers and quasiquotations, combined. * Anonymous records are cool, OCaml objects are pretty similar in that they are structurally typed. Also interestingly neither of them supports pattern matching–if I recall the F# limitation correctly. * Slices and ranges are cool, in practice there are pretty powerful and well-known OCaml libraries that provide those, e.g. https://ocaml.janestreet.com/ocaml-core/latest/doc/base/Base/Sequence/#val-range https://ocaml.janestreet.com/ocaml-core/latest/doc/base/Base...
- yawaramin 7y agoThat is by design. OCaml philosophy has been to be explicit about code. It's very Go-ish in that sense. So for example integer and float operators are different: let int3 = int1 + int2 let float3 = float1 +. float2 However OCaml does allow operator redefinition and shadowing, so you can redefine and use any operators you want just by opening their specific modules (local opens, i.e. module opens that last for the scope of a single expression, are preferred): let int3 = Int.Ops.(int1 + int2) let float 3 = Float.Ops.(float1 + float2) Note: the above modules Int.Ops and Float.Ops are for illustration purposes only, they don't exist in the standard library. You could write them pretty easily, though.
- lacampbell 7y agoMy Ocaml is rusty, but I am pretty sure you can do Int32.(3 + 2) or similar. This may be the core library though..
- dean177 7y agoThis isn’t really true. You can do: let ( + ) a b = ... Just fine
- thedufer 7y agoThat's a custom operator definition. Operator overloading is when the same operator can be used on different types (for example, if `+` worked on both floats and ints, instead of explicitly having to reach for the correct `+` like you do in OCaml).
- ernst_klim 7y agoWhich can be done, since OCaml has objects let (+) a b = a#add b
- thedufer 7y agoYeah, I guess you can get a limited sort of operator overloading by building it out of row polymorphism. That doesn't solve the most common use-case of operator overloading, though, which is allowing the same operators to work on floats and ints, because it only works on objects.