[Haskell-beginners] Record types with multiple constructors

Lyndon Maydwell maydwell at gmail.com
Sun Dec 7 09:52:47 UTC 2014


Absolutely!


Any record fields not defined for all constructors become partial.

The correct way would be to have each constructor take a distinct type (if
they are indeed distinct). Unfortunately this isn't nearly as succinct, but
it is much safer.


 - Lyndon

On Sun, Dec 7, 2014 at 8:37 PM, Derek McLoughlin <derek.mcloughlin at gmail.com
> wrote:

> Hi,
>
> Record types usually have a single constructor. I've even seen blog
> posts that suggest that they must have a single constructor. However,
> multiple constructors are allowed:
>
> data Employee = RegularEmployee {
>                                   name :: String
>                              } |
>                              Supervisor {
>                                   name :: String,
>                                   salesTarget :: Double
>                              }
>                              Manager {
>                                   name :: String,
>                                   salesTarget :: Double
>                                   budget :: Double
>                              }
>
> I don't see this used much in Haskell code - either in explanatory
> books/tutorials or in code I've examined on GitHub. Are there
> drawbacks to using multiple constructors in this way?
>
> Derek.
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20141207/3ae25ab3/attachment.html>


More information about the Beginners mailing list