Browse thread
RE: [Caml-list] F#
[
Home
]
[ Index:
by date
|
by threads
]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
| Date: | -- (:) |
| From: | Brian Hurt <bhurt@j...> |
| Subject: | Re: [Caml-list] F# |
Roland Zumkeller wrote: > Wasn't there a more or less good reason for OCaml *not* to support > operator overloading? > Yes. Ocaml has modules and functors. IMHO, any time you thing "boy, it'd be really nice to use operator overloading here", you should use a module and a functor instead. I have a long post on this topic comming soon, but for now: 1) Functors state up front what operators need to be provided to the code. This is less of a problem with purely numeric code, but I've yet to see a language that allowed operator overloading and then exercised the restraint and kept them only on numeric types. At which point, having it stated in the .mli file what operators are needed, and having it checked that you're actually providing them, is nice. 2) Functors allow for more flexible semantics as to what operations need to be provided, so operations that aren't obviously operators can be required as well- for example, Newton's method likes having an "equals within epsilon" function, or norm operation, neither of which are really operators. 3) Functors allow for more types- for example, if you're doing Newton's method on complex numbers, the type of an epsilon, or the type of a norm, is not a complex number, but instead just a real. Or if you're doing Newton's method on vectors, the type of the derivitive is a matrix. These are easy to express this with functors, hard to do with operator overloading. 4) Functors make it easier to swap in alternate implementations of various operations. For example, doing Newton's method on vectors, you end up needing to calculate b/A, where A is a matrix and b a vector. This is easy enough to translate as solving Ax=b, you standard linear system. It's nice to be able to switch out which linear system solver you want to use with functors, rather harder with operator overloading. Brian