[Haskell-cafe] OOP'er with (hopefully) trivial questions.....

Nicholls, Mark Nicholls.Mark at mtvne.com
Mon Dec 17 07:22:43 EST 2007


Thanks I need to revisit data and newtype to work out what the
difference is I think.

-----Original Message-----
From: Jed Brown [mailto:five9a2 at gmail.com] On Behalf Of Jed Brown
Sent: 17 December 2007 12:04
To: Nicholls, Mark
Cc: haskell-cafe at haskell.org
Subject: Re: [Haskell-cafe] OOP'er with (hopefully) trivial

On 17 Dec 2007, Nicholls.Mark at mtvne.com wrote:

> Ooo
> "The constructor of a newtype must have exactly one field but `R' has
> two In the newtype declaration for `Rectangle'"
> It doesn't like 
> "newtype Rectangle = R Int Int"

You want

  data Rectangle = R Int Int

A newtype declaration will be completely erased at compile time.  That
is, when you have a declaration like

  newtype Circle = C Int

the compiled code will not be able to distinguish between a Circle and
an Int.  You do, however, get all the benefits of a separate entity in
the type system.  When your type only has one constructor, newtype is
preferred over data, but they are semantically equivalent.  There are
extensions which provide impressive newtype-deriving-foo (getting the
compiler to write fairly non-trivial instance declarations for you).


More information about the Haskell-Cafe mailing list