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
[Caml-list] Single-case union types as strong typedefs
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Nathaniel Gray <n8gray@g...>
Subject: Re: [Caml-list] Single-case union types as strong typedefs
On 23 Oct 2004 15:00:02 +1000, skaller <> wrote:
> This:
>        newtype metres = float
> meaning
>        type metres = Metres float
> means you have to define all the operations on 'metres' all
> over again:
>        let (++) (Metres x) (Metres y) = Metres (x .+ y)
> I'd guess in Haskell newtype can benefit from typeclasses?

I would have to try writing some code to be sure it makes sense, but I
envisioned this as more of a contract mechanism.  It would be a way of
making "semi-opaque" values without encapsulation guarantees but also
without performance penalties.  A function that needed to do any
serious work with the underlying values would destruct the wrappers:
  let hypoteneuse (Metres x) (Metres y): metres = sqrt(x*x + y*y)

> Actually for quantities, you'd use some phantom types
> so you could represent products of the dimensions,
> such as square metres too .. so the simplistic
> constructor representation isn't very good.

I'll have to read a bit about phantom types.  Maybe they fit the bill.


>>>-- Nathaniel Gray -- Caltech Computer Science ------>
>>>-- Mojave Project -- -->

To unsubscribe, mail Archives:
Bug reports: FAQ:
Beginner's list: