[Haskell-cafe] OOP'er with (hopefully) trivial questions.....
Jules Bean
jules at jellybean.co.uk
Mon Dec 17 12:15:22 EST 2007
Peter Verswyvelen wrote:
> Very interesting, I did not know that!
>
> I thought newtype was an optimization of data, and that "newtype" was bad terminology. But if newtype is just a wrapper around a type, then the name is choosen well.
>
> I'm a bit confused why then one needs a data-constructor-like tag to construct a newtype value then? Is this to avoid having to add a type signature (for type inference)? I find this a bit weird since
>
> newtype Foo = Foo Int
> bar = Foo 123
>
> does not safe a lot of keystrokes ;) compared to
>
> -- Incorrect Haskell follows
> newtype Foo = Int
> bar = 123::Foo
You've broken the principle type property.
let's stay away from numeric literals (which are fiddly because they're
overloaded already in typeclass Num) and take a concrete type:
data Foo = A | B
newtype Bar = Bar Foo
Now, the type of "A" is "Foo". If I was allowed to write "A::Bar" then
I no longer have a simple principle type for "A": it appears that the
type of "A" is "Foo" or "Bar" depending how I annotate it.
Of course, I can make this work fine, using some form of constraint.
Haskell already has constraints, we call them classes, and I could
imagine giving "A" the principle type "(FooOrNewtypeOfFoo a) => a"
However, that's a bunch of added complexity for such a simple feature :)
Much nicer just to write "A" :: Foo and "Bar A" :: Bar.
Jules
More information about the Haskell-Cafe
mailing list