97jaz / gregor

Date and time library for Racket
45 stars 10 forks source link

Feature request: typed racket interface #52

Open joha2 opened 2 years ago

joha2 commented 2 years ago

First of all: this is a very nice package! Keep up the good work!

I'd like to use gregor in a project together with typed racket. I was able to write a (require/typed ...) interface to some of its most important functions by using opaque types and the predicates:

(require gregor)
(require gregor/period)

(require/typed gregor
               [#:opaque Date date?]
               [#:opaque DateTime datetime?]
               [#:opaque Moment moment?]
               [date (case->
                      [Integer -> Date]
                      [Integer Integer -> Date]
                      [Integer Integer Integer -> Date])]
               [date->iso8601 (-> Date String)]
...)

My problem now is, that I would like to work with period?s, too. But here the opaque type trick

(require/typed gregor/period
               [#:opaque Period period?])
(define-type DateArithmeticProvider (U DateProvider Period))
(require/typed gregor
               [+days (-> DateArithmeticProvider Integer DateArithmeticProvider)])

does not work, since using (+days (date 2222 11 1) 2) throws warnings which may in the future become errors:

period?: contract violation
  any-wrap/c: Unable to protect opaque value passed as `Any`
  value: #<date 2222-11-03>
  This warning will become an error in a future release.
  in: the 1st argument of
      a part of the or/c of
      (or/c
       struct-predicate-procedure?/c
       (-> Any boolean?))
  contract from: (interface for period?)
  blaming: <pkgs>/test/gregor_interface.rkt
   (assuming the contract is correct)
  at: <pkgs>/test/gregor_interface.rkt:52:32
#<date 2222-11-03>

where the source position gregor_interface.rkt:52:32 corresponds to the opaque type definition of Period.

I have to admit, the I am a total n00b in racket, which means I have no real understanding of the deeper mechanisms of the type system of typed/racket and its interaction with contracts.

My two questions on this issue are:

Thank you!

joha2 commented 1 year ago

Since I observed new activity in another issue, I thought I could revive this issue, too. Is there any news or comment on this matter @97jaz :smile_cat:? Sorry for bothering you :smiley_cat: