caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Dario Teixeira <darioteixeira@yahoo.com>
To: caml-list@yquem.inria.fr
Subject: Width subtyping
Date: Fri, 29 May 2009 07:10:12 -0700 (PDT)	[thread overview]
Message-ID: <327446.80988.qm@web111511.mail.gq1.yahoo.com> (raw)


Hi,

Though it is probably been-there-done-that material for the veterans in this list,
for the sake of the not-so-veterans I have to ask: how do you guys typically model
width subtyping in Ocaml?   Consider for example three record types that share some
of their fields:

type t1 = {a: int; b: int; c: int;        }
type t2 = {a: int; b: int; c: int; d: int;}
type t3 = {        b: int; c: int; d: int;}

In some circumstances, the object system can be put to good use for this kind of problem.
Not always though, so I'm curious about other approaches people use.  One approach
I've considered is to create a superset record where each field is an optional type,
and then creating constructors/getters for each valid subset.  Unfortunately this
solution feels a bit kludgy, even if it reduces code duplication.  For safety reasons
I'm therefore currently just declaring each record type independently (concerns about
duplication be damned).

In other words, polymorphic variants provide a very elegant solution for subtyping
with sum types.  Is there some brilliant idea that could do the same for product types?

Best regards,
Dario Teixeira







             reply	other threads:[~2009-05-29 14:10 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-05-29 14:10 Dario Teixeira [this message]
2009-05-29 14:21 ` [Caml-list] " Jacques Carette
2009-05-29 14:43 ` David Allsopp
2009-05-29 15:33 ` Richard Jones

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=327446.80988.qm@web111511.mail.gq1.yahoo.com \
    --to=darioteixeira@yahoo.com \
    --cc=caml-list@yquem.inria.fr \
    /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).