This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2024-04-02
Channels
- # announcements (5)
- # beginners (36)
- # biff (2)
- # calva (51)
- # clojure (12)
- # clojure-austin (7)
- # clojure-europe (11)
- # clojure-nl (1)
- # clojure-norway (63)
- # clojure-uk (2)
- # community-development (5)
- # core-typed (10)
- # datomic (9)
- # graalvm (6)
- # honeysql (1)
- # jobs (4)
- # leiningen (14)
- # london-clojurians (1)
- # lsp (23)
- # malli (88)
- # missionary (10)
- # off-topic (41)
- # practicalli (7)
- # re-frame (1)
- # reitit (5)
- # releases (2)
- # remote-jobs (1)
- # ring (11)
- # squint (2)
- # xtdb (5)
I am stuck at annotating a deftype such that it does not allow nil values e.g.
(require '[typed.clojure :as t])
(t/ann-datatype [w] NonNilDataClass [v :- w]) ;; May be I can apply :filters here?
(deftype NonNilDataClass [v])
(t/ann ->NonNilDataClass (t/All [w] [w :-> (NonNilDataClass w) :filters {:then (! nil 0) :else (is nil 0)}]))
(defn ->NonNilDataClass
[v]
(assert (nil? v) "nil supplied as value")
(NonNilDataClass. v))
(t/check-ns-clj)
;Expected result with filter {:then (! nil v__#0), :else (is nil v__#0)}, got filter {:then tt, :else tt}
;
;
;in:
;(fn*
; ([v]
; (do (assert (nil? v) "nil supplied as value") (NonNilDataClass. v))))
;
;
;
;Execution error (ExceptionInfo) at clojure.core.typed.errors/print-errors! (errors.cljc:299).
;Type Checker: Found 1 error
Got it working 😃
(t/defalias NonNil (t/TFn [x] (t/I x (t/Not nil))))
(t/ann-datatype [w] NonNilDataClass [v :- (NonNil w)])
(deftype NonNilDataClass [v])
with (NonNilDataClass. 10)
(t/check-ns-clj)
=> :ok
with (NonNilDataClass. nil)
(t/check-ns-clj)
=>
Type Error
Polymorphic function (new NonNilDataClass nil) could not be applied to arguments:
Polymorphic Variables:
w
Domains:
(fr33m0nk.typed/NonNil w)
Arguments:
nil
Ranges:
(NonNilDataClass w)
in:
(new NonNilDataClass nil)
Type Checker: Found 1 error
While I have solved my problem, I would love to know more about :filters
and when to use them
Totally forget I could use (t/I x Object)
(`null pointer exceptions` should have reminded me of this 😅 )
PS: Is there any documentation for using :filters
? Do they provide Type refinement? I can also look at examples and try making sense of it if this isn't documented.
I don't remember, but it implements "occurrence typing". Here's the original paper https://www2.ccs.neu.edu/racket/pubs/icfp10-thf.pdf
yes, it's related to type refinement. my friend Andrew also extended it to refinement types for Typed Racket https://arxiv.org/pdf/1511.07033v1.pdf