PasteRack.org
Paste # 31619
2020-06-08 14:35:15

Fork as a new paste.

Paste viewed 761 times.


Embed:

typed-make-matrix

#lang typed/racket

(require racket/vector)

(define-type (Matrixof a) (Mutable-Vectorof (Mutable-Vectorof a)))

(: make-matrix (∀ (A) Integer Integer A -> (Matrixof A)))
(define (make-matrix i j fill)
  (let ((v (make-vector i))
        (w (make-vector j fill)))
    (for ((idx (in-range 0 i)))
      (vector-set! v idx (vector-copy w)))

    ;; With cast
    ;; minimum.rkt:15:12: Type Checker: Type (Mutable-Vectorof (Mutable-Vectorof A)) could not be converted to a contract because it contains free variables.
    ;;   in: (Matrixof A)
    ;; (cast v (Matrixof A))

    ;; Without cast
    ;; minimum.rkt:15:4: Type Checker: type mismatch
    ;;   expected: (Mutable-Vectorof (Mutable-Vectorof A))
    ;;   given: (Mutable-Vectorof Any)
    v
    ))