# Type and object in Racket

**URL:** <https://racket.discourse.group/t/type-and-object-in-racket/2091>\
**Category:** General\
**Created:** [July 12, 2023, 8:40am UTC](https://racket.discourse.group/t/type-and-object-in-racket/2091 "2023-07-12T08:40:33Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![damien\_mattei](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/damien_mattei/32/2706_2.png) [@damien\_mattei](https://racket.discourse.group/u/damien_mattei)\
**Post date:** [July 12, 2023, 8:40am UTC](https://racket.discourse.group/t/type-and-object-in-racket/2091/1 "2023-07-12T08:40:33Z")

</div>

Hello,

what is the best way to define a class and to be able to derive a type associated with it? example : creating a matrix object and having a matrix? predicate to check it type.

i want to create a matrix that will be a vector of vector for example #(#(1 2) #(3 4) #( 5 6))

i find many Racket's modules :array, matrice and the class system and the language typed/racket but i do not know what is the best combination of all those Racket feature to do it. For example Guile has a Guile object system called goops that integrate all the features but instead in Racket i can not find the same level of integration.

Regards,

Damien

---

<div class="post-metadata">

**Author:** ![soegaard](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/soegaard/32/19_2.png) [@soegaard](https://racket.discourse.group/u/soegaard)\
**Post date:** [July 12, 2023, 10:10am UTC](https://racket.discourse.group/t/type-and-object-in-racket/2091/2 "2023-07-12T10:10:07Z")

</div>

It depends on what you want.

The most common way of defining a new datatype is to use a struct.

`(struct matrix (m n elements))`

This will define a predicate `matrix?` for you.

However structures aren't objects. If you want to use classes and objects, use  
the builtin class system: `(require racket/class)`.

[https://docs.racket-lang.org/guide/classes.html](https://docs.racket-lang.org/guide/classes.html)

In memory the representation of structures are more compact and you avoid the overhead of method calls. On the other hand, with class you can create subclasses, override methods and much more.

If you only need matrices over real numbers, then I'd stick to structures.

Also, consider using a single vector to hold the elements. In your example the matrix  
is `#(#(1 2) #(3 4) #( 5 6))`. It contains 4 vectors. It's much faster to allocate only one vector in a flat representation.

---

<div class="post-metadata">

**Author:** ![damien\_mattei](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/damien_mattei/32/2706_2.png) [@damien\_mattei](https://racket.discourse.group/u/damien_mattei)\
**Post date:** [July 12, 2023, 10:43am UTC](https://racket.discourse.group/t/type-and-object-in-racket/2091/3 "2023-07-12T10:43:20Z")

</div>

i find that in structure i have a built-in predicate for type like struct-name?  
but i want to be able to have method so class is needed but then with class i have no built-in predicate to test the type of class like class-name?  
that is a problem ,perheaps i can create a structure that contains a class... but this is strange.

---

<div class="post-metadata">

**Author:** ![soegaard](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/soegaard/32/19_2.png) [@soegaard](https://racket.discourse.group/u/soegaard)\
**Post date:** [July 12, 2023, 11:05am UTC](https://racket.discourse.group/t/type-and-object-in-racket/2091/4 "2023-07-12T11:05:36Z")

</div>

With an object, you have `is-a?`.

So you can write:

```scheme
(define (matrix? x)  
    (is-a? x matrix))

```

---

<div class="post-metadata">

**Author:** ![damien\_mattei](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/damien_mattei/32/2706_2.png) [@damien\_mattei](https://racket.discourse.group/u/damien_mattei)\
**Post date:** [July 12, 2023, 11:14am UTC](https://racket.discourse.group/t/type-and-object-in-racket/2091/5 "2023-07-12T11:14:40Z")

</div>

merçi , i searched but can not find it in the doc, i will use that.

---

<div class="post-metadata">

**Author:** ![soegaard](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/soegaard/32/19_2.png) [@soegaard](https://racket.discourse.group/u/soegaard)\
**Post date:** [July 12, 2023, 11:33am UTC](https://racket.discourse.group/t/type-and-object-in-racket/2091/6 "2023-07-12T11:33:37Z")

</div>

There is a whole set of "interface utilities" here:

[https://docs.racket-lang.org/reference/objectutils.html](https://docs.racket-lang.org/reference/objectutils.html)

---

<div class="post-metadata">

**Author:** ![damien\_mattei](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/damien_mattei/32/2706_2.png) [@damien\_mattei](https://racket.discourse.group/u/damien_mattei)\
**Post date:** [July 12, 2023, 3:17pm UTC](https://racket.discourse.group/t/type-and-object-in-racket/2091/7 "2023-07-12T15:17:05Z")

</div>

cool... the doc is here then.thank you Jens.

---

<div class="post-metadata">

**Author:** ![shawnw](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/shawnw/32/1031_2.png) [@shawnw](https://racket.discourse.group/u/shawnw)\
**Post date:** [July 12, 2023, 9:31pm UTC](https://racket.discourse.group/t/type-and-object-in-racket/2091/8 "2023-07-12T21:31:40Z")

</div>

In this particular case you might look at [`flomat`](https://docs.racket-lang.org/manual-flomat/index.html) or [`math/matrix`](https://docs.racket-lang.org/math/matrices.html), unless reinventing the wheel is the point.

(I've found the actual object system that comes with Racket is rarely used or even really needed. Though it does come in handy once in a while.)

---

<div class="post-metadata">

**Author:** ![damien\_mattei](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/damien_mattei/32/2706_2.png) [@damien\_mattei](https://racket.discourse.group/u/damien_mattei)\
**Post date:** [July 13, 2023, 8:36pm UTC](https://racket.discourse.group/t/type-and-object-in-racket/2091/9 "2023-07-13T20:36:42Z")

</div>

flomat seems perfect for numerical computation on matrix, even if i have vectors of vectors it could be used to create the equivalent matrix.

---

<div class="post-metadata">

**Author:** ![damien\_mattei](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/damien_mattei/32/2706_2.png) [@damien\_mattei](https://racket.discourse.group/u/damien_mattei)\
**Post date:** [July 13, 2023, 8:44pm UTC](https://racket.discourse.group/t/type-and-object-in-racket/2091/10 "2023-07-13T20:44:53Z")

</div>

i talked too fast, i can not find a predicate matrix? or flomat? in flomat and can not install it from catalog in the package manager.

---

<div class="post-metadata">

**Author:** ![damien\_mattei](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/damien_mattei/32/2706_2.png) [@damien\_mattei](https://racket.discourse.group/u/damien_mattei)\
**Post date:** [July 13, 2023, 8:47pm UTC](https://racket.discourse.group/t/type-and-object-in-racket/2091/11 "2023-07-13T20:47:22Z")

</div>

i find flomat in sci package. the install is ok now.

---

<div class="post-metadata">

**Author:** ![damien\_mattei](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/damien_mattei/32/2706_2.png) [@damien\_mattei](https://racket.discourse.group/u/damien_mattei)\
**Post date:** [July 14, 2023, 9:08am UTC](https://racket.discourse.group/t/type-and-object-in-racket/2091/12 "2023-07-14T09:08:27Z")

</div>

Jens i see you are one creator of both flomat ant math/matrix, i search a way to have a predicate matrix? for flomat (like it exists in math/matrix) or be able to initialize matrix in math/matrix with vectors of vectors (like it exists in flomat) ,unfortunately i need those two features and they exists each one in one package but never the two features are in the same library ☹

---

<div class="post-metadata">

**Author:** ![soegaard](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/soegaard/32/19_2.png) [@soegaard](https://racket.discourse.group/u/soegaard)\
**Post date:** [July 14, 2023, 9:11am UTC](https://racket.discourse.group/t/type-and-object-in-racket/2091/13 "2023-07-14T09:11:48Z")

</div>

From memory: try flomat?

---

<div class="post-metadata">

**Author:** ![damien\_mattei](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/damien_mattei/32/2706_2.png) [@damien\_mattei](https://racket.discourse.group/u/damien_mattei)\
**Post date:** [July 14, 2023, 1:07pm UTC](https://racket.discourse.group/t/type-and-object-in-racket/2091/14 "2023-07-14T13:07:25Z")

</div>

yes 🙂 ,flomat? works ,undocumented indeed (only mentionned as types in function definitions)  
thank you

---

<div class="post-metadata">

**Author:** ![soegaard](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/soegaard/32/19_2.png) [@soegaard](https://racket.discourse.group/u/soegaard)\
**Post date:** [July 14, 2023, 1:26pm UTC](https://racket.discourse.group/t/type-and-object-in-racket/2091/15 "2023-07-14T13:26:18Z")

</div>

Here:

[https://docs.racket-lang.org/manual-flomat/index.html#(def.\_((lib.\_flomat%2Fmain..rkt).\_flomat))](https://docs.racket-lang.org/manual-flomat/index.html#%28def._%28%28lib._flomat%2Fmain..rkt%29._flomat%29%29)

---

<div class="post-metadata">

**Author:** ![damien\_mattei](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/damien_mattei/32/2706_2.png) [@damien\_mattei](https://racket.discourse.group/u/damien_mattei)\
**Post date:** [July 14, 2023, 5:37pm UTC](https://racket.discourse.group/t/type-and-object-in-racket/2091/16 "2023-07-14T17:37:01Z")

</div>

ok i just understand that the struct always define a default predicate to test its type, sorry i'm not so familiar with Racket ecosystem

---

<div class="post-metadata">

**Author:** ![gus-massa](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/gus-massa/32/507_2.png) [@gus-massa](https://racket.discourse.group/u/gus-massa)\
**Post date:** [July 14, 2023, 6:11pm UTC](https://racket.discourse.group/t/type-and-object-in-racket/2091/17 "2023-07-14T18:11:58Z")

</div>

That bit me a few times. Mostly the accessors like `something-size` because it's not obvious that is not an independent function. Perhaps the doc system can be improved to show them.

---

<div class="post-metadata">

**Author:** ![benknoble](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/benknoble/32/16_2.png) [@benknoble](https://racket.discourse.group/u/benknoble)\
**Post date:** [July 14, 2023, 6:59pm UTC](https://racket.discourse.group/t/type-and-object-in-racket/2091/18 "2023-07-14T18:59:25Z")

</div>

Note that searching `flomat?` does bring you to the struct definition, and clicking `struct` in the definition clarifies that it binds `<name>?`. (I agree: when first learning Racket, this confused me. Now, the terseness is actually more helpful. If added, the accessors and such should be collapsible and collapsed by default.)

---

<div class="post-metadata">

**Author:** ![damien\_mattei](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/damien_mattei/32/2706_2.png) [@damien\_mattei](https://racket.discourse.group/u/damien_mattei)\
**Post date:** [July 15, 2023, 7:51am UTC](https://racket.discourse.group/t/type-and-object-in-racket/2091/19 "2023-07-15T07:51:40Z")

</div>

it could be good to uniformize the API of flomat and math/matrix, for example i wanted to use build-matrix:  
[7.3&nbsp;Construction](https://docs.racket-lang.org/math/matrix_construction.html#%28def._%28%28lib._math%2Fmatrix..rkt%29._build-matrix%29%29)  
to construct a flomat matrix given a procedure but it does not exist, in fact it can be done but it is a bit "tricky" (not sure the english word is relevant here...) i can do the same with define-pointwise-unary .  
given a 'uniform' function i add a "dummy" argument (initially the function has no arguments as it returns random number);

```scheme
; return a number in ]-1,1[
(define (uniform dummy) {(random) * (if {(random 2) = 0} ;; we randomly choose the sign of the random number
		         		  1
			        	  -1)})

```

and i can create the vector of flomat matrices like that (sorry the syntax is a bit Scheme+) using .uniform! :

```scheme
{M <+ (vector-ec (: n {lnc - 1}) ; vectors by eager comprehension (SRFI 42)
			  ($>
			    (define-pointwise-unary uniform)
			    (.uniform! (zeros {nc[{n + 1}]} {nc[n] + 1}))))} ;; flomat Matrix

```

the result displayed:

```scheme
Welcome to DrRacket, version 8.9 [cs].
Language: reader "../Scheme-PLUS-for-Racket/main/Scheme-PLUS-for-Racket/SRFI/SRFI-105-toplevel.rkt", with debugging; memory limit: 8192 MB.

M=#((flomat: ((0.7543054041209455 0.4965469325617333) (-0.3875771629661438 -0.9874318398967905))) (flomat: ((0.23886416495864896 0.9744680851440323 0.11934183859804237) (0.8789191729890156 0.818771282281826 -0.6626255355836153) (-0.6558813612031107 0.15529903334151 -0.29173454425315964))) (flomat: ((-0.40467338640523715 -0.4125675858962485 0.7910669635846114 -0.39687916975255766) (-0.3748689002759595 -0.28853484895435366 -0.37187524427428165 0.8346232833810251) (-0.10089962975753541 -0.06721836467772253 -0.7026817426452885 0.07562557624888604) (0.0017178627562977964 -0.9395939457313021 0.4977632229064476 -0.6135792314597592))))

```

it is a vector of flomat.

I think i can find all the stuff in flomat to build my program : now i just need to access and modify elements of flomat matrix , i will integrate the accessor and setters in Scheme+ before.

---

<div class="post-metadata">

**Author:** ![damien\_mattei](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/damien_mattei/32/2706_2.png) [@damien\_mattei](https://racket.discourse.group/u/damien_mattei)\
**Post date:** [July 15, 2023, 9:54pm UTC](https://racket.discourse.group/t/type-and-object-in-racket/2091/20 "2023-07-15T21:54:10Z")

</div>

i needed flomat? to do a bit of functional overload of operator in Scheme+:

```scheme
(define (multiply-flomat-vector M v)
  (flomat->vector (times M (matrix v))))

(overload * multiply-flomat-vector (flomat? vector?) 'operator)

;; vector overload for concatenation

;; {#(1) + #(2 3 4)}
;; '#(1 2 3 4)
(overload + vector-append (vector? vector?) 'operator)

```

used here:

```scheme
 ;; create an array with 1 in front for the bias coefficient
{z_1 <- #(1) + z[i]}

{z̃[{i + 1}] <- M[i] * z_1} ; z̃ = matrix * vector , return a vector

(display "z̃[{i + 1}] = ") (display {z̃[{i + 1}]}) (newline)

```

z̃[{i + 1}] = #(-0.4839735172378113 -0.40037108475276867 -0.34185694789193694)

works great 🙂 (but i need to rewrite overload with hashtable ,for now it is only usable at top-level or in the main module)

[Next page](https://racket.discourse.group/t/type-and-object-in-racket/2091.md?page=2)
