Version française
Home     About     Download     Resources     Contact us    
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 <skaller@users.sourceforge.net> 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.

Cheers,
-n8

-- 
>>>-- Nathaniel Gray -- Caltech Computer Science ------>
>>>-- Mojave Project -- http://mojave.cs.caltech.edu -->

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