Skip to content

Commit

Permalink
Add support for OverloadedRecordDot
Browse files Browse the repository at this point in the history
  • Loading branch information
dcastro committed Sep 22, 2023
1 parent 52ae8b0 commit 1eccd12
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 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,31 @@ 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 fields like `tztLocalTime`; it exports functions like `tzTimeLocalTime` instead.
We also export `HasField` instances to compatibility with `OverloadedRecordDot`.
## 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 alternative 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 +105,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 1eccd12

Please sign in to comment.