[Haskell-beginners] Phantom types and export lists

Karl Voelker ktvoelker at gmail.com
Sat Oct 20 18:52:39 CEST 2012


On Sat, Oct 20, 2012 at 12:41 AM, Emmanuel Surleau
<emmanuel.surleau at gmail.com> wrote:
> Is this the recommended way of doing something like this?

It looks to me like using a "smart constructor" would be sufficient in
this case. That is, you have a function:

makeTask :: String -> Either String Task

This function performs your validation. Then you make sure not to
export the data constructor for Task - that way, the only way to make
a Task outside of this module is to call the validating constructor
function.

> When I'm exporting "Task", I seem
> to be exporting the data type, but is there a way to export the Task
> constructor itself (not that I would want that)?

You can say Foo(..) to export all the data constructors of type Foo.
Or, you can list out the data constructors you want to export inside
the parens. If you want to be explicit about the fact that you are not
exporting any data constructors, you should probably write Foo().

Note that the same syntax applies to classes and their methods.

> Of course, it's quite possible that I'm reinventing the wheel and that
> I've missed a perfectly useable data validation module on hackage.

I don't know of any. But I'm also not convinced that such a thing is
necessary - smart constructors are usually all you need. Perhaps in a
context with more assumptions (i.e., the web), there would be some
useful work that a validation library could do.

-Karl



More information about the Beginners mailing list