Skip to content

Commit

Permalink
Add support for OverloadedRecordDot
Browse files Browse the repository at this point in the history
Problem: The `Data.Time.TZTime` module does not export its fields;
instead, it exports 3 functions for reading those fields:
`tzTimeLocalTime`, `tzTimeTZInfo`, `tzTimeOffset`.

We want them to behave as field-like as possible, but read-only.

However, they cannot be used with `OverloadedRecordDot`, i.e. you cannot
write something like `tz.tzTimeLocalTime`.

Solution: Add `HasField` instances.
  • Loading branch information
dcastro committed Sep 25, 2023
1 parent 2fb4102 commit df7b7da
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/Data/Time/TZTime/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import Data.Time.TZInfo (TZIdentifier, TZInfo(..), fromIdentifier)
import Data.Time.Zones (LocalToUTCResult(..))
import Data.Time.Zones qualified as TZ
import GHC.Generics (Generic)
import GHC.Records (HasField(..))
import GHC.Stack (HasCallStack)
import Text.ParserCombinators.ReadP (ReadP)
import Text.ParserCombinators.ReadP qualified as P
Expand Down Expand Up @@ -67,6 +68,38 @@ instance Show TZTime where
where
tzIdent = T.unpack $ tziIdentifier tzi

----------------------------------------------------------------------------
-- TZTime fields
----------------------------------------------------------------------------
{-
Note: We do not want users to be able to unsafely modify `TZTime`'s fields.
For that reason, the `Data.Time.TZTime` module does
not export its fields`; it exports functions like `tzTimeLocalTime` instead.
We also export `HasField` instances for compatibility with `OverloadedRecordDot`.
>>> import Data.Time.TZInfo as TZInfo
>>> tz = fromPOSIXTime (TZInfo.fromLabel TZInfo.Europe__Rome) 0
>>>
>>> :set -XOverloadedRecordDot
>>> tz.tzTimeLocalTime
1970-01-01 01:00:00
## WARNING! ##
According to <https://gitlab.haskell.org/ghc/ghc/-/wikis/records/overloaded-record-fields>,
there are plans to add `setField` to the `HasField` class, which could then
be used to implement `OverloadedRecordUpdate`.
This conflicts with our intent: we only want to support `OverloadedRecordDot`,
but NOT `OverloadedRecordUpdate`!
There are also proposals to split the `HasField` class in two.
If `setField` is indeed added to the `HasField` class, we'll have to drop these instances.
If the `HasField` class is split in two, that's not a problem.
-}

-- | The local time of this `TZTime`.
tzTimeLocalTime :: TZTime -> LocalTime
tzTimeLocalTime = tztLocalTime
Expand All @@ -79,6 +112,10 @@ tzTimeTZInfo = tztTZInfo
tzTimeOffset :: TZTime -> TimeZone
tzTimeOffset = tztOffset

instance HasField "tzTimeLocalTime" TZTime LocalTime where getField = tzTimeLocalTime
instance HasField "tzTimeTZInfo" TZTime TZInfo where getField = tzTimeTZInfo
instance HasField "tzTimeOffset" TZTime TimeZone where getField = tzTimeOffset

----------------------------------------------------------------------------
-- Constructors
----------------------------------------------------------------------------
Expand Down

0 comments on commit df7b7da

Please sign in to comment.