Symbolic array type representation

Description

Background

Clojure has long supported string encoded array type hints only by using the Java internal class name:

  • "[Ljava.lang.String;" - 1D array of strings

  • "[[D" - 2D array of primitive doubles

and so on.

This syntax is verbose (requires fully-qualified class name), prone to error (due to weird syntax) and non-obvious for users, as it requires arcane JVM String patterns.

Problem

There is no symbolic representation for array types, nor one that doesn’t require familiarity with JVM name munging.

Approach

Add support for a symbol comprising the name of the array component type (primitive, full-qualified class, or import aliases accepted) as the symbol namespace and a dimension component of a single digit in the range 1-9 as the symbol name. This symbolic representation should work for both type hinting and as a class value.

Examples:

  • java.lang.String/1 or String/1 - 1D array of strings

  • double/2 - 2D array of primitive doubles

The component class may be fully qualified or short-form. In the latter case, the class alias is resolved at compilation time.

Mechanically, the symbolic array type hint will expand into the string encoded form in the compiler to leverage the existing class resolution logic. In syntax quote contexts, the array component class symbol will expand into its package qualified form if resolvable to a class.

The existing support for internal class name strings and special primitive array symbols (like doubles) will not be removed, but the new array type symbols should now be the primary one as this works in both value and type hint position.

Alternative prior approach

Originally, we solved this problem by recognizing array type symbols with trailing stars corresponding to the dimensionality. However, unlike Clojure namespaces, the JVM classpath space is not arbitrated, therefore adding * suffixes to symbols operates in an open environment and clashed with existing JVM class names (such as those generated during deftype definition). This is detailed in https://clojure.atlassian.net/browse/CLJ-2833 .

Patch: clj-2807-15.patch (requires revert of clj-2807-9.patch from master first - commit= b2a91435)

Screened by: Alex Miller

Environment

None

Attachments

19
  • 12 Apr 2024, 05:29 PM
  • 11 Apr 2024, 12:57 PM
  • 10 Apr 2024, 04:00 PM
  • 09 Apr 2024, 02:18 PM
  • 25 Mar 2024, 02:02 PM
  • 20 Mar 2024, 07:33 PM
  • 01 Feb 2024, 06:21 PM
  • 01 Feb 2024, 05:08 PM
  • 10 Jan 2024, 05:03 PM
  • 09 Jan 2024, 02:25 PM
  • 03 Jan 2024, 05:18 PM
  • 03 Jan 2024, 03:08 PM

Activity

Show:

Alex Miller April 30, 2024 at 1:08 PM

Released in Clojure 1.12.0-alpha10

Michael Fogus April 11, 2024 at 12:57 PM

-14 throws return of sneakyThrow

Michael Fogus April 10, 2024 at 4:01 PM

-13 uses array sym regex in matchSymbol plus has name changes and condition tightening.

Michael Fogus March 25, 2024 at 2:03 PM

-11 includes whitespace cleanup and method access changes.

Alex Miller February 8, 2024 at 10:38 PM

Released in 1.12.0-alpha6

Fixed

Details

Assignee

Reporter

Approval

Ok

Patch

Code and Test

Priority

Affects versions

Fix versions

Created October 12, 2023 at 6:07 PM
Updated April 30, 2024 at 1:08 PM
Resolved April 30, 2024 at 1:08 PM