Skip to content

Completing CLR interop

dmiller edited this page Sep 13, 2010 · 6 revisions

Generally, CLR interop works the same as JVM interop. One piece not addressed elsewhere:

Multi-dimensional arrays

The JVM does not have true multi-dimensional arrays, just ragged arrays. The core Clojure functions that manipulate multi-dimensional arrays assume raggedness.

The CLR of course has ragged arrays, but it also supports true (rectangular) multi-dimensional arrays. In the implementation of the core Clojure functions on the CLR, we assumed ragged arrays. Thus, we have no support for true multi-dimensional arrays.

The functions of interest are:

  • (aget array idx+) — Returns the value at the index/indices. Works on arrays of all types.
  • (aset array idx+ val) — Sets the value at the index/indices. Works on arrays of reference types. Returns val.
  • (make-array class dim+) — Creates and returns an array of instances of the specified class of the specified dimension(s).

We could easily overload make-array to take a second argument of a vector of ints specifying the dimensions. Thus:

(make-array Int32 4 5 6)  ; => a ragged array
(make-array Int32 [4 5 6])  ; => a multi-dimensional array

Or we could just have a new function called make-multidim-array.

For aget and aset, I think overloading them in this way would not be advised due to performance implications. We can expect these functions to be called in tight loops. Better to introduce new functions:

(aget-md array idx+)
(aset-md  array idx+)

We would also need to introduce equivalents to aset-int, etc.

I’m open to suggestions on names.