2010/8/10 Tim Daly <d...@axiom-developer.org>

> This code uses the JGraphT graphing package which
> wants to create a graph where the types of the
> vertices and edges are generics, as in:
>
> JGraphT<MyVertexType,MyEdgeType> graph = ....
>
> and I'm at a loss for how to instantiate a graph in clojure.


Tim, Generics in java are there only for making the java compiler happy (err
sorry, in order for the compiler to help you write more robust code ;-) ).
They are lost (erased) at runtime, even in "pure java".

So you just don't need to care for generics when instanciating objects from
clojure, even with java interoperability in mind.

If the JGraphT constructor takes say 2 args arg1 and arg2, you'll just write

(let [graph (JGraphT. arg1 arg2)] ....) and every side will be happy:
clojure, and java.


>
>
> Armando Blancas wrote:
>
>> If you must use Java's HashSet, you can use it untyped:
>>
>> user=> (deftype MyType [])
>> user.MyType
>> user=> (def my-set (HashSet.))
>> #'user/my-set
>> user=> (.add my-set (MyType.))
>> true
>> user=> (.add my-set (java.util.Date.))
>> true
>>
>> If you need to enforce the use of a type, use a checked set:
>>
>> user=> (def checked (java.util.Collections/checkedSet my-set MyType))
>> #'user/checked
>> user=> (.add checked (MyType.))
>> true
>> user=> (.add checked (java.util.Date.))
>> java.lang.ClassCastException: Attempt to insert class java.util.Date
>> element
>> into collection with element type class user.MyType (NO_SOURCE_FILE:0)
>>
>> On Aug 10, 1:40 pm, Tim Daly <d...@axiom-developer.org> wrote:
>>
>>
>>> I have java code that reads:
>>>
>>> Set<MyType> s = new HashSet();
>>>
>>> I don't know how to write the parameter <MyType> in clojure
>>> where MyType is a Java class.
>>>
>>>
>>>
>>> Laurent PETIT wrote:
>>>
>>>
>>>> 2010/8/10 Tim Daly <d...@axiom-developer.org
>>>> <mailto:d...@axiom-developer.org>>
>>>>          How do I construct
>>>>     Set<MyType> s = new HashSet();
>>>>    in clojure?
>>>>      Can you be more precise about the context of what you're trying to
>>>> achieve ?
>>>>      'cause the naive answer to your question is
>>>>      (def s #{})
>>>> or
>>>> (let [s #{}] ...)
>>>>      but I think you have a usecase in mind I'm not clearling seeing yet
>>>> from your question ...
>>>>      --
>>>> You received this message because you are subscribed to the Google
>>>> Groups "Clojure" group.
>>>> To post to this group, send email to clojure@googlegroups.com
>>>> Note that posts from new members are moderated - please be patient
>>>> with your first post.
>>>> To unsubscribe from this group, send email to
>>>> clojure+unsubscr...@googlegroups.com<clojure%2bunsubscr...@googlegroups.com>
>>>> For more options, visit this group at
>>>> http://groups.google.com/group/clojure?hl=en- Hide quoted text -
>>>>
>>>>
>>> - Show quoted text -
>>>
>>>
>>
>>
>>
>
> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clojure@googlegroups.com
> Note that posts from new members are moderated - please be patient with
> your first post.
> To unsubscribe from this group, send email to
> clojure+unsubscr...@googlegroups.com<clojure%2bunsubscr...@googlegroups.com>
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en
>

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to