[Haskell-cafe] Unwrapping newtypes

Evan Laforge qdunkan at gmail.com
Wed Sep 8 13:51:21 EDT 2010


Here's a different approach:

If you just want to make the typechecker distinguish between some
values but still have some functions that don't care what "subtype"
they are, you can use phantom types:

data Obj x = Obj X Y Z

data BlogObj
type Blog = Obj BlogObj

data CommentObj
type Comment = Obj CommentObj

data UserObj
type User = Obj UserObj

Now  you can write a function that takes a Blog, and it will refuse
other kinds of 'Obj'.  But you can still write a function that takes
'Obj x' and it will accept any kind of Obj.  So you don't need to do
any unwrapping if you leave the functions that matter polymorphic.


More information about the Haskell-Cafe mailing list