6.6 Types, Predicates and Accessors
syntax
(Array A)
> (define arr (array #[1 2 3 4 5]))
> arr - : (Array Positive-Byte)
(array #[1 2 3 4 5])
> (ann arr (Array Real)) - : (Array Real)
(array #[1 2 3 4 5])
> (ann arr (Array Any)) - : (Array Any)
(array #[1 2 3 4 5])
> (define arr (mutable-array #[1 2 3 4 5]))
> arr - : (Mutable-Array Integer)
(mutable-array #[1 2 3 4 5])
> (ann arr (Array Real)) - : (Array Real)
(mutable-array #[1 2 3 4 5])
> (ann arr (Array Any)) - : (Array Any)
(mutable-array #[1 2 3 4 5])
syntax
(Settable-Array A)
> (define arr (mutable-array #[1 2 3 4 5]))
> arr - : (Mutable-Array Integer)
(mutable-array #[1 2 3 4 5])
> (ann arr (Settable-Array Integer)) - : (Settable-Array Integer)
(mutable-array #[1 2 3 4 5])
> (ann arr (Settable-Array Real)) eval:93:0: Type Checker: Expected (Settable-Array Real), but
got (Mutable-Array Integer)
in: Real
syntax
(Mutable-Array A)
> (define arr (mutable-array #[#[1 2] #[3 4]]))
> (vector-set! (mutable-array-data arr) 0 -10)
> arr - : (Mutable-Array Integer)
(mutable-array #[#[-10 2] #[3 4]])
syntax
Example: | |||
|
syntax
Examples: | |||||||||
|
This makes indexes-accepting functions easier to use, because it is easier to convince Typed Racket that a vector contains Integer elements than that a vector contains Index elements.
> (define js ((inst vector Index) 3 4 5))
> js - : Indexes
'#(3 4 5)
> (ann js (Vectorof Integer)) eval:103:0: Type Checker: Expected (Vectorof Integer), but
got Indexes
in: Integer
> (ann js In-Indexes) - : In-Indexes
'#(3 4 5)
procedure
v : Any
procedure
(settable-array? v) → Boolean
v : Any
procedure
(mutable-array? v) → Boolean
v : Any
> (: maybe-array-data (All (A) ((Array A) -> (U #f (Vectorof A)))))
> (define (maybe-array-data arr) (cond [(mutable-array? arr) (mutable-array-data arr)] [else #f])) eval:106:0: Type Checker: Polymorphic function
`mutable-array-data' could not be applied to arguments:
Argument 1:
Expected: (Mutable-Array A)
Given: (Struct Mutable-Array)
in: #f
> array? - : (Any -> Boolean : (Array Any))
#<procedure:Array?>
> settable-array? - : (Any -> Boolean : (Struct Settable-Array))
#<procedure:Settable-Array?>
> mutable-array? - : (Any -> Boolean : (Struct Mutable-Array))
#<procedure:Mutable-Array?>
procedure
(array-shape arr) → Indexes
arr : (Array A)
Examples: | ||||||||||||
|
procedure
(array-size arr) → Index
arr : (Array A)
Examples: | ||||||||||||
|
procedure
(array-dims arr) → Index
arr : (Array A)
procedure
(mutable-array-data arr) → (Vectorof A)
arr : (Mutable-Array A)