[Haskell-beginners] Date type needs helper function?
Matthew Low
mlow at ualberta.ca
Thu Jul 1 17:41:11 UTC 2021
> why can Book take everything naked but my Day version not?
The Day data type is actually just a newtype wrapper around an Int, which
encodes the number of days since some point in the past - the docs more
details:
https://hackage.haskell.org/package/time-1.12/docs/Data-Time-Calendar.html.
So to use the data constructor directly as in the Book example, you'd have
to give it the number of days directly, something like ModifiedJulianDay
1234. The helper function gives a more convenient way to construct a Day. I
believe this general approach of having functions to help you construct a
data type is called 'smart constructors'. They're more common when the data
constructors are not exported from a module, so the helper functions are
the only way to create a value of that type - useful if you have some
constraints you need enforced on your type.
On Thu, Jul 1, 2021 at 10:41 AM Galaxy Being <borgauf at gmail.com> wrote:
> I am using the built-in data type Day (Data.Time) in two ways
>
> data PDate = PDate Day
>
> or
>
> type PDate Day
>
> doesn't seem to matter. But then this doesn't work
>
> testrec1 = PDate 2021 7 1
>
> I always must use the "helper function" fromGregorian
>
> testrec0 = PDate (fromGregorian 2021 7 1)
> ...
> PDate 2021-07-01
>
> Looking at Real World Haskell examples
>
> data BookInfo = Book Int String [String] deriving (Show)
> ...
> myInfo = Book 9780135072455 "Algebra of Programming" ["Richard Bird",
> "Oege de Moor"]
>
> I know there's a great Haskell lesson to learn here, so why can Book take
> everything naked but my Day version not?
>
>
> ⨽
> Lawrence Bottorff
> Grand Marais, MN, USA
> borgauf at gmail.com
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20210701/1989032d/attachment.html>
More information about the Beginners
mailing list