caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Peng Zang <peng.zang@gmail.com>
To: caml-list@yquem.inria.fr
Cc: "Jim Farrand" <jim@farrand.net>
Subject: Re: [Caml-list] Typeclasses in OCaml (Was: Haskell vs OCaml)
Date: Thu, 14 Aug 2008 11:21:22 -0400	[thread overview]
Message-ID: <200808141121.25463.peng.zang@gmail.com> (raw)
In-Reply-To: <e16c7bcd0808140746r4cd42fedo4fb6474d687274c4@mail.gmail.com>

-----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-----


  reply	other threads:[~2008-08-14 15:21 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-08-14 14:46 Jim Farrand
2008-08-14 15:21 ` Peng Zang [this message]
2008-08-14 15:38   ` [Caml-list] " Jon Harrop
2008-08-14 16:04   ` Jim Farrand
2008-08-14 17:13     ` Peng Zang
2008-08-14 18:37     ` Till Varoquaux
2008-08-15 11:03       ` Wolfgang Lux
2008-08-14 20:53 ` Nathaniel Gray
2008-08-14 22:33   ` Erik de Castro Lopo
2008-08-15  0:21 ` Brian Hurt

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=200808141121.25463.peng.zang@gmail.com \
    --to=peng.zang@gmail.com \
    --cc=caml-list@yquem.inria.fr \
    --cc=jim@farrand.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).