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

Jed Brown jed at 59A2.org
Mon Dec 17 07:04:08 EST 2007

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

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 196 bytes
Desc: not available
Url : http://www.haskell.org/pipermail/haskell-cafe/attachments/20071217/d8acdaa4/attachment.bin

More information about the Haskell-Cafe mailing list