magic
magic copied to clipboard
by-ref issue
In normal Clojure, this works:
(let [m (new |System.Collections.Generic.Dictionary`2[System.Object,System.Object]|)
v :initial]
(if (.TryGetValue m :hi (by-ref v))
v
:nothing))
;; =>
;; :nothing
In magic, it doesn't:
(m/faster
(let [m (new |System.Collections.Generic.Dictionary`2[System.Object,System.Object]|)
v :initial]
(if (.TryGetValue m :hi (by-ref v))
v
:nothing)))
;; =>
;; ArgumentException Don't know how to create ISeq from: System.Int64 clojure.lang.RT.seqFrom (:0)
The first part of this is the generic type syntax. MAGIC supports System.Collections.Generic.Dictionary[System.Object,System.Object]|
, which is more convenient as you don't have to type `2
. It is a bug that MAGIC does not support the more verbose syntax though.
Given that, it's a type issue
(m/faster
(let [m (new System.Collections.Generic.Dictionary|[System.Object,System.Object]|)
v :initial]
(if (.TryGetValue m :hi (by-ref v))
v
:nothing)))
;; System.Exception: Cannot convert clojure.lang.Keyword& to System.Object&
The fix is
(m/faster
(let [m (new System.Collections.Generic.Dictionary|[clojure.lang.Keyword,clojure.lang.Keyword]|)
v :initial]
(if (.TryGetValue m :hi (by-ref v))
v
:nothing)))
;; :nothing
MAGIC does not upcast right now. Types have to be exact matches, but that restriction can and should be relaxed.