Clojure programmers often want to use Java methods in higher-order functions (e.g. passing a Java method to map). Until now, this has required programmers to manually wrap methods in functions. This is verbose, and might require manual hinting for overload disambiguation, or incur incidental reflection or boxing.

With this release, programmers can now use Java qualified method symbols as ordinary functions in value contexts - the compiler will automatically generate the wrapping function. Method symbols signifying values must resolve to a single method at compile time, using the new qualified method symbols and/or :param-tags metadata as necessary.

Qualified method symbols have value semantics when used in non-invocation positions:

  • Classname/method - value is a Clojure function that invokes a static or instance method

  • Classname/new - value is a Clojure function that invokes a constructor

Uniform qualified method syntax - Class/method and Class/new

Java members inherently exist in a class. For methods as values we need a way to explicitly specify the class of an instance method because there is no possibility for inference.

The Classname/method syntax can now be used for static methods and instance methods, and Classname/new for constructors, in both invocation and value position. Class qualifiers may be either full class names or imported "short" class names. In all cases other than static method invocation, the combination of Class, method name and :param-tags must unambiguously resolve to exactly one method. Given a fully resolved qualified method symbol, the compiler does no inference from target type, arg types or arity and reflection will not occur.

Note: Static fields are values and should be referenced without parens unless they are intended as function calls, e.g (System/out) should be System/out. Future Clojure releases will treat the field’s value as something invokable and invoke it.

:param-tags metadata

When using methods as values, only the class and method names are provided in the symbol, if the method is overloaded the parameter types must be provided to resolve to one specific method.

:param-tags metadata can now be supplied on qualified method symbols to specify the signature of a single desired method ('resolving' it). The :param-tags metadata is a vector of zero or more tags: […​ tag …​]. A tag is any existing valid :tag metadata value. Each tag corresponds to a parameter in the desired signature (arity should match the number of tags). Parameters with non-overloaded types can use the placeholder _ in lieu of the tag.

A new metadata reader syntax ^[ …​ ] attaches :param-tags metadata to member symbols, just as ^tag attaches :tag metadata to a symbol.

Array class symbols

Clojure supports class symbols both as a value (for class object) and as a type hint, but has not provided syntax for array classes other than strings.

Clojure now provides array class symbols, comprising the name of the array component type: primitive, fully-qualified class, or import class, followed by an asterisk for each dimension of the array (1 or more). Array class symbols can be used as type hints, or as values that resolves to the corresponding array class object.

Examples: String*, java.lang.String*, long**.