[Haskell-beginners] Phantom types and export lists

Emmanuel Surleau emmanuel.surleau at gmail.com
Sat Oct 20 21:52:29 CEST 2012

Karl Voelker <ktvoelker at gmail.com> wrote:

>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

l see. However, with phantom types, you get more flexibility. For instance, you can generate a blank Task as a template. I think I want to retain the ability to work  with non-well-formed data. But I'll keep smart constructors in mind.

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

 Thanks. This means I can export Sanitise (..) instead of Sanitise and sanitise.

>> Of course, it's quite possible that I'm reinventing the wheel and
>> 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.

I guess you are right. Not many use cases for a completely generic validation library.



Sent from my Android phone with K-9 Mail. Please excuse my brevity.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20121020/ea750c97/attachment-0001.htm>

More information about the Beginners mailing list