I think this is roughly what you want:
#lang racket
(define/contract (safe-hash-set h . args)
(->* (hash?) () #:rest (letrec ([c (recursive-contract (or/c null?
(cons/c any/c (cons/c any/c c))))]) c) hash?)
...)
(safe-hash-set (hash) 1)
Sam
On Thu, Aug 24, 2017 at 2:08 PM, David Storrs <[email protected]> wrote:
> I'd like to be able to write a function that makes it easy to set one or
> more keys in a hash, without worrying about whether the hash is mutable or
> not. It could be called in any of the following ways:
>
> (define imm-h (hash 'a 1))
> (define mut-h (make-hash '((a . 1))))
>
> (safe-hash-set imm-hash 'x 2) ; returns (hash 'a 1 'x 2)
> (safe-hash-set imm-hash 'x 2 'y 7) ; returns (hash 'a 1 'x 2 'y 7)
>
> (safe-hash-set mut-hash 'x 2) ; returns (begin (hash-set! mut-h 'x 2)
> mut-h)
> (safe-hash-set mut-hash 'x 2 'y 7) ; returns (begin (hash-set! mut-h 'x 2)
> (hash-set! mut-h 'y 7) mut-h)
>
> This definition isn't quite what I need:
> (define (safe-hash-set h . args)
> (->* (hash?) () #:rest (non-empty-listof any/c) hash?)
> ...)
>
> I want to be able to say "the rest argument is a list that has an even
> number of elements and more than zero elements", but I'm not sure how to
> capture that. Thoughts?
>
> Dave
>
> --
> 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 [email protected].
> For more options, visit https://groups.google.com/d/optout.
--
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 [email protected].
For more options, visit https://groups.google.com/d/optout.