Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
Typeclasses in OCaml (Was: Haskell vs OCaml)
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2008-08-14 (15:21)
From: Peng Zang <peng.zang@g...>
Subject: Re: [Caml-list] Typeclasses in OCaml (Was: Haskell vs OCaml)
Hash: SHA1

On Thursday 14 August 2008 10:46:41 am Jim Farrand wrote:
> Things like the (=) operator in OCaml vex me.  One of the big
> advantages of static typing and type inference is that stupid
> programmer errors are prevented at compile time.  However, the (=)
> operator in OCaml is effectively meaningless for a lot of types, yet
> there is no way to prevent a programmer from accidentally calling it.
> One way to get around this would be to take away (=) and (==) and
> replace them with specific versions for each type (just like we already
> have (+) and (+.) etc) but this leads to really verbose code.
> Type classes solve this kind of problem very effectively.
> Regards,
> Jim

- From what I was told earlier on this list, if you want type classes in OCaml 
you go with objects.  So you would not have:

  (=) : 'a -> 'a -> bool

But instead:

  (=) : (#equatable as 'a) -> 'a -> bool


  class type equatable = object
    method equals : 'self -> bool

This gives all the advantages of static typing and type inference and prevents 
stupid errors and it is meaningful for all types that it is implemented for.

Version: GnuPG v2.0.7 (GNU/Linux)