# Making nested vectors with build-vector in typed/racket

**URL:** <https://racket.discourse.group/t/making-nested-vectors-with-build-vector-in-typed-racket/2183>\
**Category:** Questions & Answers\
**Tags:** typed-racket\
**Created:** [August 1, 2023, 2:18am UTC](https://racket.discourse.group/t/making-nested-vectors-with-build-vector-in-typed-racket/2183 "2023-08-01T02:18:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![srikumarks](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/srikumarks/32/2633_2.png) [@srikumarks](https://racket.discourse.group/u/srikumarks)\
**Post date:** [August 1, 2023, 2:18am UTC](https://racket.discourse.group/t/making-nested-vectors-with-build-vector-in-typed-racket/2183/1 "2023-08-01T02:18:20Z")

</div>

I can't seem to get how to get the use of build-vector in the following code to pass the Racket type checker. I've reduced this from an existing untyped function that constructs an "image" as a "vector of vector of color" by reading from a file. Tried "Mutable-Vectorof" to closely match what is stated as "Expected" in the error message, but I get the same error with "Vectorof" instead too. It looks like some kind of limitation with deeper types, but the default language is /deep anyway, so not sure. Any pointers please?

Just copy-pasting the following code into Racket and hitting "Run" should produce the error.

```scheme
#lang typed/racket

(struct (t) color ([a : t]
                   [r : t]
                   [g : t]
                   [b : t]))

(: mk-image (-> Integer Integer (Mutable-Vectorof (Mutable-Vectorof (color Flonum)))))
(define (mk-image width height)
  (build-vector height
                (λ ([r : Index])
                  (vector (color 0.0 0.0 0.0 0.0)))))

```

---

<div class="post-metadata">

**Author:** ![srikumarks](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/srikumarks/32/2633_2.png) [@srikumarks](https://racket.discourse.group/u/srikumarks)\
**Post date:** [August 1, 2023, 2:46am UTC](https://racket.discourse.group/t/making-nested-vectors-with-build-vector-in-typed-racket/2183/2 "2023-08-01T02:46:50Z")

</div>

In particular, the error on the second argument says -

```scheme
Argument 2:
  Expected: (-> Index a)
  Given: (-> Index (Mutable-Vector (color Flonum-Positive-Zero)))

```

It appears that the (Mutable-Vector...) part ought to match with the "a" type variable, but the checker complains. And similarly for the result vector -

```scheme
Result type: (Mutable-Vectorof a)
Expected result: (Mutable-Vectorof (Mutable-Vectorof #(struct:color (Flonum Flonum Flonum Flonum))))

```

Again, a ought to have matched against `(Mutable-Vectorof #(struct:color (Flonum Flonum Flonum Flonum))))` which is the same as `(Mutable-Vectorof (color Flonum))` but it doesn't seem to.

---

<div class="post-metadata">

**Author:** ![srikumarks](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/srikumarks/32/2633_2.png) [@srikumarks](https://racket.discourse.group/u/srikumarks)\
**Post date:** [August 1, 2023, 2:49am UTC](https://racket.discourse.group/t/making-nested-vectors-with-build-vector-in-typed-racket/2183/3 "2023-08-01T02:49:33Z")

</div>

I noticed something after posting the previous reply 😃 -

Adding the following return type annotation to the λ results in passing the type checker.

```scheme
 : (Mutable-Vectorof (color Flonum))

```

So `(Mutable-Vectorof (color Flonum))` doesn't match against the inferred `(Mutable-Vector (color Flonum))`. ... which also seems weird. However, I actually have a function that produces a `Mutable-Vectorof` in my main code.

---

<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:** [August 1, 2023, 2:55am UTC](https://racket.discourse.group/t/making-nested-vectors-with-build-vector-in-typed-racket/2183/4 "2023-08-01T02:55:08Z")

</div>

Using [`inst`](https://docs.racket-lang.org/ts-reference/special-forms.html#%28form._%28%28lib._typed-racket%2Fbase-env%2Fprims..rkt%29._inst%29%29) with `build-vector` is another way of telling TR what the correct type should be:

```scheme
(: mk-image (-> Integer Integer (Mutable-Vectorof (Mutable-Vectorof (color Flonum)))))
(define (mk-image width height)
  ((inst build-vector (Mutable-Vectorof (color Flonum)))
   height
   (λ ([r : Index])
     (vector (color 0.0 0.0 0.0 0.0)))))

```

* * *

`build-vector` is a parameterized type

```scheme
> (:print-type build-vector)
(All (a) (-> Integer (-> Index a) (Mutable-Vectorof a)))

```

and `inst` is used to bind `a` to a concrete type so TR doesn't have to try to infer what it is (I've found its type inference leaves something to be desired compared to the Hindley-Milner systems I'm used to.)

---

<div class="post-metadata">

**Author:** ![srikumarks](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/srikumarks/32/2633_2.png) [@srikumarks](https://racket.discourse.group/u/srikumarks)\
**Post date:** [August 1, 2023, 4:22am UTC](https://racket.discourse.group/t/making-nested-vectors-with-build-vector-in-typed-racket/2183/5 "2023-08-01T04:22:23Z")

</div>

Hah! Thanks. Yeah that works and "new" trick learnt. It sure feels like there are gaps. Here, for example, `(Mutable-Vector x)` should be able to match against `(Mutable-Vectorof x)`.
