chansey97
2021-9-13 14:16:34

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.


samth
2021-9-13 14:19:09

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).


soegaard2
2021-9-13 14:19:35

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


soegaard2
2021-9-13 14:20:42

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.


chansey97
2021-9-13 14:21:48

@soegaard2 @samth Thanks!


samth
2021-9-13 14:22:51

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))


chansey97
2021-9-13 14:26:04

Thanks!