syntax for covariance defn?

36 views
Skip to first unread message

Colin Yates

unread,
Mar 9, 2015, 5:26:26 PM3/9/15
to clojure-c...@googlegroups.com
If I have:

(t/defalias Persistent (t/HMap :mandatory {:id Number}))

and

(t/defalias Person (t/HMap :mandatory {:title String ... }))
(t/defalias Car (t/HMap :mandatory {:engine String ... }))

then I want to do the following

(t/defn persist 
  [thing-to-be-persisted <the-type magic-syntax-to-get-type>]  :- (t/I the-type Persistent)
  (assoc thing-to-be-persisted :id 1))

I can't quite figure out the syntax - is this even possible or can't persist by typed this way?

Thanks!

Ambrose Bonnaire-Sergeant

unread,
Mar 9, 2015, 5:29:09 PM3/9/15
to core.typed
Try something like

(t/defn [[a :< (t/Map Any Any)]] persist [m :- a] :- (t/Assoc m ':id Number),
  (assoc m :id 1))

Untested.

Ambrose Bonnaire-Sergeant

unread,
Mar 9, 2015, 5:30:26 PM3/9/15
to core.typed
Rather

(t/defn [[a :< (t/Map Any Any)]] persist [m :- a] :- (t/Assoc a ':id Number),
  (assoc m :id 1))

Colin Yates

unread,
Mar 9, 2015, 5:51:37 PM3/9/15
to clojure-c...@googlegroups.com
That gives:

(t/defn [[a :< (t/Map Any Any)]] persist [m :- a] :- (t/Assoc a ':id Number),
  (assoc m :id 1))
ExceptionInfo core.typed Not Yet Implemented Error:(:<NO LINE>) Poly for defn  clojure.core/ex-info (core.clj:4403)
clojure.lang.ExceptionInfo: core.typed Not Yet Implemented Error:(:<NO LINE>) Poly for defn
 at clojure.core$ex_info.invoke (core.clj:4403)
    clojure.core.typed.errors$nyi_error.invoke (errors.clj:151)
    clojure.core.typed.macros$defn.doInvoke (macros.clj:247)
    clojure.lang.RestFn.invoke (RestFn.java:636)
    clojure.lang.Var.invoke (Var.java:430)
    clojure.lang.AFn.applyToHelper (AFn.java:195)
    clojure.lang.Var.applyTo (Var.java:700)
    clojure.lang.Compiler.macroexpand1 (Compiler.java:6552)
    clojure.lang.Compiler.macroexpand (Compiler.java:6613)
    clojure.lang.Compiler.eval (Compiler.java:6687)
    clojure.lang.Compiler.eval (Compiler.java:6666)
    clojure.core$eval.invoke (core.clj:2927)
    clojure.main$repl$read_eval_print__6625$fn__6628.invoke (main.clj:239)
    clojure.main$repl$read_eval_print__6625.invoke (main.clj:239)
    clojure.main$repl$fn__6634.invoke (main.clj:257)
    clojure.main$repl.doInvoke (main.clj:257)
    clojure.lang.RestFn.invoke (RestFn.java:1523)
    clojure.tools.nrepl.middleware.interruptible_eval$evaluate$fn__11025.invoke (interruptible_eval.clj:67)
    clojure.lang.AFn.applyToHelper (AFn.java:152)
    clojure.lang.AFn.applyTo (AFn.java:144)
    clojure.core$apply.invoke (core.clj:624)
    clojure.core$with_bindings_STAR_.doInvoke (core.clj:1862)
    clojure.lang.RestFn.invoke (RestFn.java:425)
    clojure.tools.nrepl.middleware.interruptible_eval$evaluate.invoke (interruptible_eval.clj:51)
    clojure.tools.nrepl.middleware.interruptible_eval$interruptible_eval$fn__11067$fn__11070.invoke (interruptible_eval.clj:183)
    clojure.tools.nrepl.middleware.interruptible_eval$run_next$fn__11060.invoke (interruptible_eval.clj:152)
    clojure.lang.AFn.run (AFn.java:22)
    java.util.concurrent.ThreadPoolExecutor.runWorker (ThreadPoolExecutor.java:1145)
    java.util.concurrent.ThreadPoolExecutor$Worker.run (ThreadPoolExecutor.java:615)
    java.lang.Thread.run (Thread.java:745)

Ambrose Bonnaire-Sergeant

unread,
Mar 9, 2015, 5:58:59 PM3/9/15
to core.typed
(ann persist (All [[m :< (Map Any Any)]] [m -> (Assoc m ':id Number)])
(core/defn persist [x] (assoc x :id 1))

Colin Yates

unread,
Mar 9, 2015, 6:18:42 PM3/9/15
to clojure-c...@googlegroups.com
That parses but gives check-ns errors. The form in question is:

(t/ann new-ar (t/All [[m :< (t/Map t/Any t/Any)]] [m -> (t/Assoc m ':id Number ':version Number)]))
(defn new-ar
[detail]
{:post [(not (nil? (:id %)))
(zero? (:version %))]}
(assoc detail :id 10 :version 0))

The errors are:

Type Error (qfi/health/core/domain/model/common.clj:40:3) Cannot assoc args `[(clojure.core.typed/Val :id) {:then tt, :else ff}] [(clojure.core.typed/Val 10) {:then tt, :else ff}] [(clojure.core.typed/Val :version) {:then tt, :else ff}] [(clojure.core.typed/Val 0) {:then tt, :else ff}]` on m
in: (assoc detail :id 10 :version 0)

Ambrose Bonnaire-Sergeant

unread,
Mar 9, 2015, 7:00:18 PM3/9/15
to core.typed
Ah damn. Seems to not work on multiple entries. Please file a bug.

Colin Yates

unread,
Mar 10, 2015, 5:43:05 AM3/10/15
to clojure-c...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages