Skip to content

Commit

Permalink
Finish fixing #80
Browse files Browse the repository at this point in the history
  • Loading branch information
cgrand committed May 20, 2022
1 parent e5fb8b3 commit 79e9bfe
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
24 changes: 23 additions & 1 deletion clj/src/cljd/compiler.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -1192,6 +1192,21 @@
[dc-Null (dissoc type :nullable)]
[type])))

(defn- nullable-type? [type]
(or (:nullable type)
(case (:canon-qname type)
dc.dynamic true
da.FutureOr (recur (first (:type-parameters type)))
false)))

(defn- positive-type
[{:keys [canon-qname nullable] :as type}]
(case canon-qname
dc.dynamic dc-Object
da.FutureOr (let [[t] (:type-parameters type)]
(assoc da-FutureOr :type-parameters [(positive-type t)]))
(dissoc type :nullable)))

(defn is-assignable?
"Returns true when a value of type value-type can be used as a value of type slot-type."
[slot-type value-type]
Expand Down Expand Up @@ -1256,9 +1271,16 @@
(magicast dart-expr expected-type (:dart/type (infer-type dart-expr)) env))
([dart-expr expected-type actual-type env]
(cond
(is-assignable? expected-type actual-type) dart-expr
(is-assignable? expected-type actual-type) dart-expr ; <1>
(and (= (:canon-qname expected-type) 'dc.double)
(= (:canon-qname actual-type) 'dc.int)) dart-expr
(and (nullable-type? expected-type) (nullable-type? actual-type))
(with-lifted [dart-expr dart-expr] env
(list 'dart/if (list 'dart/. nil "!=" dart-expr)
; by construction expected-type can't be dynamic or FutureOr<dynamic>, otherwise
; it would have matched the assignability test <1> above
(magicast dart-expr (positive-type expected-type) actual-type env)
nil))
;; When inlined #dart[], we keep it inlines
;; TODO: don't like the (vector? dart-expr) check, it smells bad
(and (= 'dc.List (:canon-qname expected-type) (:canon-qname actual-type))
Expand Down
3 changes: 2 additions & 1 deletion clj/test/cljd/test_clojure/core_test_cljd.cljd
Original file line number Diff line number Diff line change
Expand Up @@ -425,4 +425,5 @@
(let [x (CastNullable.)]
(is (= "clojuredart" (.text x (cond-> ["coucou"]
true (conj "clojuredart")))))
(is (= "worked" (.text x nil)))))
(is (= "worked" (.text x nil)))
(is (= "worked" (.text x ^List? (identity nil))))))

0 comments on commit 79e9bfe

Please sign in to comment.