-
Notifications
You must be signed in to change notification settings - Fork 161
Specifying types
Standard Clojure allows use of the symbols int
, double
, float
, etc. in type hints to refer to the corresponding primitive types. ClojureCLR allows this and extends this to the numeric types present in the CLR but not in the JVM: uint
, ulong
, etc. Similarly, the shorthand array references such ints
and doubles
work, and are joined by uints
, ulongs
, etc.
Aside from these special cases, Clojure uses symbols to name types in two ways:
- a package-qualified symbol (one containing periods internally) is taken to name the Java class with the same character sequence
- a namespace may contain a mapping from a symbol to a Java class, via
import
Resolving a symbol is the process of determining the value of a symbol during evaluation.
Identifying types via symbol names works reasonably well for Java because package-qualified class names are lexically compatible with symbols.
Not so for the CLR. Fully-qualified type names can contain an assembly identifier, which involves spaces and commas. Thus, fully-qualified type names cannot be represented as symbols. Generic type names include angle brackets and backquotes, and thus also are not representable as symbols. In fact, CLR typenames can contain arbitrary characters. Backslashes can escape characters that do have special meaning in the typename syntax (comma, plus, ampersand, asterisk, left and right square bracket, left and right angle bracket, backslash).
ClojureCLR extends the reader syntax for symbols to accommodate CLR type names by the mechanism of vertical bar quoting. See Reader extension: |-quoting
With this mechanism we can make a symbol referring to a type such as:
(|com.myco.mytype+nested, MyAssembly, Version=1.3.0.0, Culture=neutral, PublicKeyToken=b14a123334343434|/DoSomething x y)
or
(reify
|AnInterface`2[System.Int32,System.String]|
(m1 [x] ...)
I2
(m2 [x] ...))
Special note should be made of the proper way to refer to generic types (instantiated or not).
|System.Collections.Generic.IList`1[System.Int32]|
This is the official CLR way of referring to the type that would be referred to in C# (with using
) by IList<int>
. We do not implement C# or Visual Basic lexical conventions for type names.
Note that you need to specify System.Int32
, not just Int32
as the type parameter.