Hi, I have a question: Can Racket check type equality? (define v1 #f)
(define v2 1)
(define v3 2)
(equal-type? v1 v2) ; => #f
(equal-type? v2 v3) ; => #t
Thanks.
Racket doesn’t have a single notion of “type”. Furthermore, it’s not always possible to get the “type” of some values (see the discussion of inspectors and structs in the documentation).
Not in general. However, if your values have a type that description
can handle, you can try (equal? (description v1) (description v2))
. https://docs.racket-lang.org/describe/index.html#%28def._%28%28lib._describe%2Fmain..rkt%29._description%29%29
Most likely it would be better to write a custom type-of
that knows how to handle the range of values, you need in your use case.
@soegaard2 @samth Thanks!
The following function does something like what you want, but beware that it mostly returns #f
on things that you might think are the same type (like your example): > (define (equal-type? a b)
(define-values (a-t a-skip?) (struct-info a))
(define-values (b-t b-skip?) (struct-info b))
(if (and a-t b-t (not a-skip?) (not b-skip?) (eq? a-t b-t)) #t #f))
Thanks!