Version française
Home     About     Download     Resources     Contact us    
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: -- (:)
From: Peng Zang <peng.zang@g...>
Subject: Re: [Caml-list] Typeclasses in OCaml (Was: Haskell vs OCaml)
-----BEGIN PGP SIGNED MESSAGE-----
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

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.


Peng
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.7 (GNU/Linux)

iD8DBQFIpE11fIRcEFL/JewRAmyEAKCIbPDMFVh+zuCQ5uD/t+FNPXRJ2gCgwTTl
ZELl4dYgZvnh8cAhlnN2gb0=
=7pTp
-----END PGP SIGNATURE-----