# Getting the type of a (class) expression

**URL:** <https://racket.discourse.group/t/getting-the-type-of-a-class-expression/1302>\
**Category:** Questions & Answers\
**Tags:** typed-racket\
**Created:** [September 15, 2022, 3:57pm UTC](https://racket.discourse.group/t/getting-the-type-of-a-class-expression/1302 "2022-09-15T15:57:03Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![scolobb](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/scolobb/32/108_2.png) [@scolobb](https://racket.discourse.group/u/scolobb)\
**Post date:** [September 15, 2022, 3:57pm UTC](https://racket.discourse.group/t/getting-the-type-of-a-class-expression/1302/1 "2022-09-15T15:57:03Z")

</div>

Hi,

Consider the following typed class definition:

```scheme
(define my-class
  (class object%
    (super-new)
    (init-field [x : Integer])
    (define/public (get-x) x)))

```

I can easily get the type of this class in the REPL:

```scheme
> my-class
- : (Class (init (x Integer)) (field (x Integer)) (get-x (-> Integer)))
#<class:my-class>

```

I would like to give a name to this class expression, so that I can have a shorter version. I can obviously write the following:

```scheme
(define-type MyClassType (Class (init (x Integer)) (field (x Integer)) (get-x (-> Integer))))

```

but this would mean that I'd need to do some copying and pasting every time I change `my-class`.

Is there a better way to define this type synonym?

More generally, is there a way to get the type of an expression?

As an example, I would like to be able to write something like:

```scheme
(define x ...)
(define-type TypeOfX (get-type x))

```

---

<div class="post-metadata">

**Author:** ![hendrikboom3](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/hendrikboom3/32/2748_2.png) [@hendrikboom3](https://racket.discourse.group/u/hendrikboom3)\
**Post date:** [September 21, 2022, 3:48pm UTC](https://racket.discourse.group/t/getting-the-type-of-a-class-expression/1302/2 "2022-09-21T15:48:56Z")

</div>

There must be a way to get the type of a class because typed Racket manages to figure it out internally.

That said, I'm no further along than you.  
I'm trying to get the type of the objects in a class.  
The obvious notation is just to use the name of the class as the type of its objects. In your case that would be 'my-class', but that diesn't work. Classes are run-time values.  
I can write (Object (init (x Integer)) (field (x Integer)) (get-x (-\> Integer)))  
much like what you have done, but that has the same probems you have.

---

<div class="post-metadata">

**Author:** ![scolobb](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/scolobb/32/108_2.png) [@scolobb](https://racket.discourse.group/u/scolobb)\
**Post date:** [September 21, 2022, 9:21pm UTC](https://racket.discourse.group/t/getting-the-type-of-a-class-expression/1302/3 "2022-09-21T21:21:18Z")

</div>

Thank you for your message @hendrikboom3 , I am happy to not be the only one bugged by this question 😃

I always hesitate to tag people working on Typed Racket when writing such messages, but maybe I should to help them see the post?..

---

<div class="post-metadata">

**Author:** ![alexh](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/alexh/32/315_2.png) [@alexh](https://racket.discourse.group/u/alexh)\
**Post date:** [September 22, 2022, 10:35am UTC](https://racket.discourse.group/t/getting-the-type-of-a-class-expression/1302/4 "2022-09-22T10:35:23Z")

</div>

you can use `:print-type` to print the type of a value, but looking at its implementation, it seems to use the internal representation of the types which would not be suitable for use directly from Typed Racket programs.

I suspect there is no `get-type` facility in Typed Racket and I am not sure that it would be easy to implement.

On the other hand, I am not sure that defining types in terms of values is a good idea. If you had a `get-type` function and you define types such as `(define-type TypeOfX (get-type x))`, the actual type will silently change when the implementation of `x` changes, which might not be what the programmer intended and could hide programming errors.

Alex.

---

<div class="post-metadata">

**Author:** ![scolobb](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/scolobb/32/108_2.png) [@scolobb](https://racket.discourse.group/u/scolobb)\
**Post date:** [September 22, 2022, 11:14am UTC](https://racket.discourse.group/t/getting-the-type-of-a-class-expression/1302/5 "2022-09-22T11:14:26Z")

</div>

> [@alexh](#):
>
> you can use `:print-type` to print the type of a value, but looking at its implementation, it seems to use the internal representation of the types which would not be suitable for use directly from Typed Racket programs.

Indeed. In particular, the docs are clear about the fact that `:print-type` only works in the REPL.

> [@alexh](#):
>
> On the other hand, I am not sure that defining types in terms of values is a good idea. If you had a `get-type` function and you define types such as `(define-type TypeOfX (get-type x))`, the actual type will silently change when the implementation of `x` changes, which might not be what the programmer intended and could hide programming errors.

You are right, and I started realizing it after sending the message. On the other hand, the first question of having a shortcut for a class/instance type is still valid in the following sense. Typed `struct`s introduce a type name, which you can then use in function types, etc. Typed `struct`s even have the `#:type-name` argument which allows changing the type name. I have so far managed to find no such thing for classes. For example, the following does not work:

```scheme
(: frobnicate (-> my-class% Void))
(define (frobnicate x) (void))

```

```scheme
; /home/scolobb/Candies/prj/racket/dds/tmp.rkt:9:18: Type Checker: parse error in type;
; type name `my-class%' is unbound
; in: my-class%

```

Consider in contrast a typed `struct`:

```scheme
(struct test ([x : Integer]))

(: frobnicate-struct (-> test Void))
(define (frobnicate-struct x) (void))

```

I can of course make `frobnicate` work by spelling out the full type `(Instance (Class … ))`, or by defining a type synonym for this expression (that's what I do now), but it would be immensely practical to just have that type synonym out of the box, like it already happens with typed `structs`.
