magic icon indicating copy to clipboard operation
magic copied to clipboard

by-ref issue

Open timsgardner opened this issue 7 years ago • 1 comments

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)

timsgardner avatar Nov 16 '17 22:11 timsgardner

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.

nasser avatar Nov 17 '17 03:11 nasser