caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Alessandro Baretta <alex@baretta.com>
To: Martin Jambon <m.jambon@ibcp.fr>, ocaml <caml-list@inria.fr>
Subject: Re: [Caml-list] Type aliases
Date: Thu, 28 Nov 2002 19:55:41 +0100	[thread overview]
Message-ID: <3DE666AD.9000705@baretta.com> (raw)
In-Reply-To: <Pine.LNX.4.40.0211281805450.1077-100000@pc-bioinfo1.ibcp.fr>



Martin Jambon wrote:

> 3) Prohibit the mixing of different types that do not derive from each
>    other, without an explicit cast:
> 
>      let my_mistake = year + month   (* Type error! *)
> 
>    where year has type year and month has type month.
>    But this could be valid:
> 
>      let some_int = (year :> int) * 13 + month
> 

When I want this kind of property I use variant tags to 
encapsulate the datum and document its semantics:

type time_value = [
| `Year of int
| `Month of int
| `Week of int
| `Day of int
| `Hour of int
| `Min of int
| `Sec of int
]


let this_year = `Year(2002)

let time_value_function = function
| `Year (y) -> ...
| `Month(m) -> ...
...

This is a fairly heavy solution in terms of code length, but 
it does guarantee type safety and excellent level of 
readability--a property you'd lose with operator overloading.

Alex

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


      parent reply	other threads:[~2002-11-28 18:54 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-11-28 18:01 Martin Jambon
2002-11-28 18:51 ` Alain Frisch
2002-11-29 14:03   ` Martin Jambon
2002-11-28 18:55 ` Alessandro Baretta [this message]

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=3DE666AD.9000705@baretta.com \
    --to=alex@baretta.com \
    --cc=caml-list@inria.fr \
    --cc=m.jambon@ibcp.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).