How do I provide a name as both a match expander and contracted function?

22 Aufrufe
Direkt zur ersten ungelesenen Nachricht

Jack Firth

ungelesen,
30.12.2019, 22:07:4330.12.19
an Racket Users
If I'm making my own data types, one common thing I want to do is make smart constructors that double as match expanders. I could use `define-match-expander` with two transformers, but then I can't add a contract to the smart constructor with `contract-out`. If I try to write this:

(provide
 (contract-out
  [foo (->* (widget?) (#:name symbol?) foo?)]))

(define (make-foo widget #:name [name #f])
  ...)

(define-match-expander foo
  (syntax-parser [(_ widget) #'(? foo? (app foo-widget widget))])
  (syntax-parser [(_ arg ...) #'(make-foo arg ...)]))

...then the use of `contract-out` hides the fact that `foo` is a match expander. What should I do?

Sorawee Porncharoenwase

ungelesen,
30.12.2019, 22:33:4930.12.19
an Jack Firth, Racket Users
IIUC, this is what Alex Knauth did in https://github.com/mbutterick/txexpr/pull/8.

--
You received this message because you are subscribed to the Google Groups "Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to racket-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/racket-users/2dac85e9-8058-437c-b5bc-a92f659c02f6%40googlegroups.com.

Eric Griffis

ungelesen,
31.12.2019, 14:11:3031.12.19
an Racket Users, Jack Firth
Hi Jack,

It can be done with make-rename-transformer.

(define (foo? obj) (equal? obj foo-impl))
(define foo-widget list)

(define/contract (foo-impl _) (-> number? symbol?) 'a-foo)


(define-match-expander foo
  (syntax-parser [(_ widget) #'(? foo? (app foo-widget widget))])
  (make-rename-transformer #'foo-impl))

; (foo #t) ; contract violation

(foo 123)

Eric


On Mon, Dec 30, 2019 at 7:07 PM Jack Firth <jackh...@gmail.com> wrote:
--
Allen antworten
Antwort an Autor
Weiterleiten
0 neue Nachrichten