Replies: 3 comments
-
There is a fairly general mechanism for constructing types already in place in ClojureCLR. Check out ClrTypeSpec. This code has no problem parsing a string such as |Dictionary[int, String]| This would be parsed as a type named
|System.Collections.Generic.Dictionary[System.Int32, System.String]| fails because of the missing `2 after
|System.Collections.Generic.Dictionary[Int32, String]| This seems odd because they do work at the REPL directly: user=> Int32
System.Int32
user=> (class Int32)
System.RuntimeType
|Dictionary[int, ints]| At minimum, our improved type naming system should address each of these problems. What else would you like to see? |
Beta Was this translation helpful? Give feedback.
-
Given that I have to code any solution we come up with, I'd like to minimize that effort. :) Let's start with the second problem above, as that is the simplest to solve. (And the solution to this one will have an effect when it comes to solving the first problem above.)
|System.Collections.Generic.Dictionary`2[Int32, String]| Why does this work at the REPL? user=> Int32
System.Int32
user=> (class Int32)
System.RuntimeType The reason is that namespaces have mappings from symbols to values. When (def v 7) in the current namespace, a mapping is established from the v ; => 7
Why does user=> Int32
System.Int32 work? When a namespace is created, it is set up with set of default mappings. The code runs through loaded assemblies and looks for all public classes/interfaces/value-types with a namespace of The simplest solution to our problem is to have the method |System.Collections.Generic.Dictionary`2[Int32, String]| we would find and use We could precede this lookup by comparing the typename against the small list of bespoke type-hint symbols: |System.Collections.Generic.Dictionary`2[int, long]| This solves Problem #3 in the list above. To implement this is the addition of just a few lines of code. |
Beta Was this translation helpful? Give feedback.
-
Let's look at the first problem: Instead of |System.Collections.Generic.Dictionary`2[System.Int32, System.String]| we'd like to write |Dictionary[int, String]| If we implement our solution to problem #2 -- looking up symbols in the namespace mapping -- all we really need to do is find way to map from the symbol We can almost do this already. The (import '[System.Collections.Generic |Dictionary`2|]) and (import `[System.Collections.Generic |Dictionary`2[System.Object,System.String]|]) will also (try to) map to the type using I hesitate to change the current behavior of Solution #1: provide a new function for introducing type aliases into the current namespace. (add-type-alias 'Dictionary |System.Collections.Generic.Dictionary`2| )
(add-type-alias 'Dictionary |System.Collections.Generic.Dictionary`2| some-namespace) BTW, this would allow a sequence such as this: (add-type-alias 'Dictionary |System.Collections.Generic.Dictionary`2| )
(add-type-alias 'DictObjStr' |Dictionary[Object,String]|)
(add-type-alias 'DictObjObj' |Dictionary[Object,Object]|) Then you could do something like: (.Add ^DictObjObj mydict "help" "me") Note: this is quite easy to implement. (defn add-type-alias
([sym type] (add-type-alias sym type *ns*))
([sym type ns] (.importClass #^clojure.lang.Namespace ns sym type))) Solution #2: provide some additional syntactic sugaring to Note: if we modify (import `[System.Collections.Generic [ |Dictionary`2| :as Dictionary ]) Rather than using the (unqualified) typename, we provide the alias by wrapping the entry in brackets and adding a Though this is possible, I really hesitate to make any changes to |
Beta Was this translation helpful? Give feedback.
-
The current mechanism for specifying types using
Symbol
s is cumbersome. It follows the CLR conventions, which are not user-friendly.|System.Collections.Generic.Dictionary`2[System.Int32, System.String]|
The goal: To be able to write something like
This would require some type of
import
statement that would map the nameDictionary
to the desired class.Beta Was this translation helpful? Give feedback.
All reactions