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 (16:04)
From: Jim Farrand <jim@f...>
Subject: Re: [Caml-list] Typeclasses in OCaml (Was: Haskell vs OCaml)
2008/8/14 Peng Zang <>:

>  (=) : 'a -> 'a -> bool
> But instead:
>  (=) : (#equatable as 'a) -> 'a -> bool
> where
>  class type equatable = object
>    method equals : 'self -> bool
>  end
> 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.

This doesn't answer my question at all.  :)

 Is there any theoretical reason they couldn't added?  The kind of
answer I'm looking for is "There is no theoretical reason why not", or
"This is impossible as it would cause typing in OCaml to become
undecidable, due to interactions with other features of the OCaml type
system which aren't present in Haskell."

Though, to address your solution, I am of course aware of it, but it
has what seem like big disadvantages:

1. Every value in OCaml would then have to be an object
2. Every comparison now requires a relatively expensive dynamic
dispatch, when the correct function could be determined at runtime.
3. If I add a new operator that wasn't thought of by the language
implementors, it can't be easily added to primitive values, without
either subclassing all of them, or changing the definition in the
standard library to add the new method.