Send Beginners mailing list submissions to
[email protected]
To subscribe or unsubscribe via the World Wide Web, visit
http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
[email protected]
You can reach the person managing the list at
[email protected]
When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."
Today's Topics:
1. Record types with multiple constructors (Derek McLoughlin)
2. Re: Record types with multiple constructors (Michael Snoyman)
3. Re: Record types with multiple constructors (Lyndon Maydwell)
----------------------------------------------------------------------
Message: 1
Date: Sun, 7 Dec 2014 09:37:11 +0000
From: Derek McLoughlin <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: [Haskell-beginners] Record types with multiple constructors
Message-ID:
<CAAw9fmnNRWoOuFa0Oq0+KTWK1doin82M0jz+QRz06ZrOQnhR=q...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8
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.
------------------------------
Message: 2
Date: Sun, 07 Dec 2014 09:47:33 +0000
From: Michael Snoyman <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Record types with multiple
constructors
Message-ID:
<CAKA2JgL5Vn21ZKYxWuhxNJXtBpsOwA9Rt8MVRiPZH1=o2xv...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
Yes: it becomes really easy to write partial/broken programs, e.g.:
let myEmployee = RegularEmployee "Alice"
...
supervisor = myEmployee { salesTarget = 5.4 }
If you want to have both multiple constructors *and* multiple fields per
constructor, I'd recommend one of the following:
1. Don't name the fields.
2. Use another type in between that has only one constructor, e.g. `data
Supervisor = Supervisor { name :: String, salesTarget :: Double }`. A great
example of this is the Node datatype[1] from xml-types.
[1]
http://www.stackage.org/haddock/2014-11-27-ghc78-exc-1/xml-types-0.3.4/Data-XML-Types.html#t:Node
On Sun Dec 07 2014 at 11:37:16 AM Derek McLoughlin <
[email protected]> 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
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20141207/030625db/attachment-0001.html>
------------------------------
Message: 3
Date: Sun, 7 Dec 2014 20:52:47 +1100
From: Lyndon Maydwell <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Record types with multiple
constructors
Message-ID:
<cam5qztybssici0ohxizgdewbh3ex79vb3xct4qr2ezrtu-m...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
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 <[email protected]
> 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
> [email protected]
> 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-0001.html>
------------------------------
Subject: Digest Footer
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
------------------------------
End of Beginners Digest, Vol 78, Issue 2
****************************************