tekul / jose-jwt

Haskell implementation of JOSE/JWT standards
BSD 3-Clause "New" or "Revised" License
35 stars 22 forks source link

Give `IntDate` a `Num` instance #14

Closed folsen closed 6 years ago

folsen commented 7 years ago

Since IntDate is a newtype of POSIXTime and POSIXTime has the Num class, I think you should derive Num for IntDate as well.

My use case is I want to be able to compare times like this (iat is within 30 seconds of now):

currentTime <- getPOSIXTime
if abs(iat - (IntDate currentTime)) < 30
  ...

but since there's no Num for IntDate, I have to do this:

currentTime <- getPOSIXTime
let ahead30s = IntDate (currentTime + 30)
    behind30s = IntDate (currentTime - 30)
if behind30s < iat && iat < ahead30s
  ...
tekul commented 7 years ago

Wouldn't it be simpler to pattern match on the IntDate? That way you can use

currentTime <- getPOSIXTime
let IntDate iat' = iat
if abs (iat' - currentTime) < 30
  ...
folsen commented 7 years ago

It's a matter of preference I suppose, I just thought it made sense that IntDate would have a Num instance since it's a newtype over a Num type.

tekul commented 7 years ago

If I remember, the only reason for adding that type was to avoid an orphan instance for ToJSON and FromJSON for POSIXTime. I don't really like it much and it's something I'd like to be able to change easily in future with minimal impact, so I'd prefer to avoid encouraging its use much beyond encoding and decoding. Pattern-matching as above seems more or less as straightforward as your original code.